HttpClient4.5.2

package com.xweisoft.sms;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.commons.httpclient.Cookie;
import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;

public class ClientMultipartFormPost {
	public static void get() {
		// 定义httpClient的实例
                //若要保持session 只能创建一次HttpClient实例
		HttpClient httpclient = new HttpClient();
		// GetMethod getMethod = new GetMethod( "url");
		GetMethod getMethod = new GetMethod("url");
		// 使用系统提供的默认恢复策略
		getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
				new DefaultHttpMethodRetryHandler());// 创建get方法的实例
		// getMethod.getParams().setParameter("User-Agent",
		// "Mozilla/4.0 (compatible; MSIE 5.0; Windows XP; DigExt)");
		// httpclient = (HttpClient) request.getSession().getAttribute(
		// "HTTPCLIENT");

		// 创建post方法实例
		// PostMethod postMethod = new UTF8PostMethod("url");
		// PostMethod postMethod = new UTF8PostMethod( "url");
		
		//
		// // 填入各个表单域的值
		// NameValuePair[] data = { new NameValuePair("apiId", "A21"),
		// new NameValuePair("account", "1801.....3"),
		// new NameValuePair("code", "0610"),
		// new NameValuePair("newPwd", "111111"),
		// new NameValuePair("idCard", "") };
		//
		// // 将表单的值放入到post方法中
		// postMethod.setRequestBody(data);
		//
		// postMethod.getParams().setParameter("http.protocol.cookie-policy",
		// CookiePolicy.BROWSER_COMPATIBILITY);
		// postMethod.setRequestHeader("Referer", "url");
		try {
			// 执行GET方法
			int statusCode = httpclient.executeMethod(getMethod);
			// 获取cookies
			Cookie[] cookies = httpclient.getState().getCookies();
			for (Cookie cookie : cookies) {
				System.out.println("cookiesName----" + cookie.getName());
				System.out.println("cookiesvalue----" + cookie.getValue());
			}
			httpclient.getState().addCookies(cookies);

			// 执行post方法
			// int statusCode = httpclient.executeMethod(postMethod);
			// if (statusCode == HttpStatus.SC_MOVED_TEMPORARILY) {
			// Header locationHeader = postMethod
			// .getResponseHeader("Location");
			// String location = null;
			// if (locationHeader != null) {
			// location = locationHeader.getValue();
			// }
			// postMethod = new PostMethod(location);
			// postMethod
			// .setRequestHeader("Referer", "url");
			// NameValuePair[] data1 = { new NameValuePair("apiId", "A21"),
			// new NameValuePair("account", "1801...3"),
			// new NameValuePair("code", "0610"),
			// new NameValuePair("newPwd", "111111"),
			// new NameValuePair("idCard", "") };
			// postMethod.setRequestBody(data1);
			// postMethod.getParams().setParameter(
			// "http.protocol.cookie-policy",
			// CookiePolicy.BROWSER_COMPATIBILITY);
			// int statusCode1 = httpclient.executeMethod(postMethod);
			// if (statusCode1 != HttpStatus.SC_OK) {
			// System.out.println("Method is wrong "
			// + postMethod.getStatusLine());
			// }
			// }
			if (statusCode != HttpStatus.SC_OK) {
				System.out.println("Method is wrong "
						+ getMethod.getStatusLine());
				// System.out.println("Method is wrong "
				// + postMethod.getStatusLine());
			}
			InputStream responseBody = getMethod.getResponseBodyAsStream();
			// InputStream responseBody = postMethod.getResponseBodyAsStream();
			BufferedReader reader = new BufferedReader(new InputStreamReader(
					responseBody, "utf-8"));
			String line = reader.readLine();

		} catch (HttpException e) {
			// TODO: handle exception
			e.printStackTrace();
		} catch (IOException e) {
			// TODO: handle exception
			e.printStackTrace();
		} finally {
			getMethod.releaseConnection();// 释放链接
			// postMethod.releaseConnection();
		}
	}

	// Inner class for UTF-8 support
	public static class UTF8PostMethod extends PostMethod {
		public UTF8PostMethod(String url) {
			super(url);
		}

		@Override
		public String getRequestCharSet() {
			// return super.getRequestCharSet();
			return "UTF-8";
		}

	}
}

相关推荐