zoukankan      html  css  js  c++  java
  • 初试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>
    
  • 相关阅读:
    菜鸟快速自学java00之变量类型
    php 接口和抽象类
    java 三大特性之多态性
    设计模式
    依赖注入
    Java设计模式工厂模式
    php 设计模式之策略模式
    大数的概述
    熟悉常用的Linux操作
    GridView动态添加列
  • 原文地址:https://www.cnblogs.com/qwop/p/6637244.html
Copyright © 2011-2022 走看看