• 初试HttpClient




    package com.tan.http;
    
    import java.io.IOException;
    import java.io.UnsupportedEncodingException;
    import java.util.ArrayList;
    import java.util.List;
    
    import org.apache.http.Header;
    import org.apache.http.HeaderElement;
    import org.apache.http.HttpResponse;
    import org.apache.http.NameValuePair;
    import org.apache.http.client.ClientProtocolException;
    import org.apache.http.client.CookieStore;
    import org.apache.http.client.entity.UrlEncodedFormEntity;
    import org.apache.http.client.methods.HttpPost;
    import org.apache.http.cookie.Cookie;
    import org.apache.http.impl.client.DefaultHttpClient;
    import org.apache.http.message.BasicNameValuePair;
    import org.apache.http.protocol.HTTP;
    
    public class TestHttpClient {
    	private static final String LOGINURL = "http://localhost:8080/login.jsp";
    	private static final String USERNAME = "username";
    	private static final String PASSWORD = "password";
    	private static final String LINE = System.getProperty("line.separator");
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) throws Exception {
    		CookieStore store = getCookieStore();
    		if (store != null) {
    			List<Cookie> cookies = store.getCookies();
    			System.out.println(cookies);
    //			for (Cookie cookie: cookies) {
    //				System.out.println(cookie);
    //			}
    		}
    	}
    	private static CookieStore getCookieStore() throws UnsupportedEncodingException,
    			IOException, ClientProtocolException {
    		CookieStore store = null;
    		DefaultHttpClient httpClient = new DefaultHttpClient();
    		HttpPost httpPost = new HttpPost(LOGINURL);
    		
    		// 登录的参数 
    		List<NameValuePair> nvps = new ArrayList<NameValuePair>();
    		
    		// 登录的 Username
    		nvps.add(new BasicNameValuePair("username", USERNAME));
    		nvps.add(new BasicNameValuePair("password", PASSWORD));
    		nvps.add(new BasicNameValuePair("URL", "/"));
    		
    		httpPost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
    		
    		// 执行登录
    		HttpResponse response = httpClient.execute(httpPost);
    		// 获取 Http响应头中的 Set-Cookie
    		Header header = response.getFirstHeader("Set-Cookie");
    		HeaderElement[] elements = header.getElements();
    		if (elements != null) {
    			for (HeaderElement element : elements) {
    				System.out.println(
    							"HeaderElement's name : " + element.getName() + LINE + 
    							"HeaderElement's value : " + element.getValue() 
    							);
    				
    				// 获取 Cookies 
    				store = httpClient.getCookieStore();
    				break;
    			}
    		}
    		return store;
    	}
    
    }
    


    <%@ page contentType="text/html;charset=gb18030" pageEncoding="gb18030"%>
    <%@ page import="java.util.*"%>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <HTML>
     <HEAD>
      <TITLE> Login Form </TITLE>
      <META NAME="Generator" CONTENT="EditPlus">
      <META NAME="Author" CONTENT="">
      <META NAME="Keywords" CONTENT="">
      <META NAME="Description" CONTENT="">
      <SCRIPT LANGUAGE="JavaScript">
      <!--
    	
      //-->
      </SCRIPT>
     </HEAD>
    
     <BODY>
      <%!
    	private static boolean isEmpty(String v) {
    		return v == null || v.trim().length() == 0;
    	}
      %>
    
    
      <%
    	String username = request.getParameter("username");
    	String password = request.getParameter("password");
    	if (!isEmpty(username) && !isEmpty(password)) {
    		out.println("Login successful");
    	} else {
    		out.println("Login failure");
    	}
      %>
      <FORM METHOD="post" ACTION="login.jsp"  id="loginForm">
    	<input type="text" name="username" />
    	<input type="password" name="password"/>
    	<input type="submit" value="submit" />
    	<input type="reset" value="reset" />
      </FORM>
     </BODY>
    </HTML>
    
  • 相关阅读:
    NOIP 2011 提高组 计算系数(vijos 1739)(方法:二项式定理)
    NOIP 2012 提高组 借教室(vijos 1782) 总览
    NOIP 2012 提高组 借教室(vijos 1782) 线段树85分打法
    NOIP 2011 提高组 铺地毯(vijos 1736)(方法:纯枚举)
    获取指定时间的前一天、后一天及当前时间的前一周、前一个月
    input file禁用手机本地文件选择,只允许拍照上传图片
    给定一个时间,获取该时间所在周的周一及周日
    Fiddler手机抓包软件简单使用将h5效果显示在手机
    解决Linux服务器更换IP后,ssh连接被拒绝问题
    解决Hadoop启动报错:File /opt/hadoop/tmp/mapred/system/jobtracker.info could only be replicated to 0 nodes, instead of 1
  • 原文地址:https://www.cnblogs.com/qwop/p/6637244.html
Copyright © 2020-2023  润新知