使用Apache HttpClient访问JSP发送GET和POST请求

本示例说明了如何使用Apache HttpClient发送GET和POST请求,以及如何发送表单数据,详见代码及说明。对于ASP.NET的网站请求,可以查看使用Apache HttpClient访问ASP.NET发送GET和POST请求

 

说明:

1、发送表单POST请求时,需要借助HttpWatch或有这种功能的小工具来查看发给服务器的参数有哪些;

2、对于POST请求的表单数据,需要创建BasicNameValuePair对象的集合,然后构造成UrlEncodedFormEntity对象,再赋值到HttpPost对象的Entity中;

3、请求发送后可以使用response2.getStatusLine().getStatusCode(),获取HTTP的状态码;

4、使用HttpResponse对象的getHeaders("")或getAllHeaders方法可以获取相应头;

5、若要获取相应的内容,可以使用HttpResponse的getEntity()获取HttpEntity对象,然后再调用它的getContent()方法,获取一个输出流(InputStream对象)

 

Apache HttpClient 版本 4.0.3

下载地址:http://hc.apache.org/downloads.cgi

 

package com.zywang.http;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

/**
 * 使用HttClient访问JSP示例
 * @author ZYWANG
 */
public class HttpClientForJSP {
	public static void main(String[] args)
			throws ClientProtocolException, IOException {
		HttpClient client = new DefaultHttpClient();
		get(client,"http://localhost:8080/PMS");
		loginPost(client);
		client.getConnectionManager().shutdown();
	}

	/**
	 * 模拟POST请求
	 * @param client
	 * @throws UnsupportedEncodingException
	 * @throws IOException
	 * @throws ClientProtocolException
	 */
	private static void loginPost(HttpClient client)
			throws UnsupportedEncodingException, IOException,
			ClientProtocolException {
		System.out.println("==========登录:====================================");
		HttpPost post = new HttpPost("http://localhost:8080/PMS/LoginCheck.do");
		//创建表单参数列表
		List<NameValuePair> qparams = new ArrayList<NameValuePair>();
		qparams.add(new BasicNameValuePair("method", "check"));
		qparams.add(new BasicNameValuePair("userid", "zywang"));
		qparams.add(new BasicNameValuePair("userpassword", "111111"));
		//填充表单
		post.setEntity(new UrlEncodedFormEntity(qparams,"UTF-8"));
		
		HttpResponse response2 = client.execute(post);
		int statusCode = response2.getStatusLine().getStatusCode();
		System.out.println("statusCode:"+statusCode);
		if(statusCode==200){
			HttpEntity entity2 = response2.getEntity();
			BufferedReader reader2 = new BufferedReader(
					new InputStreamReader(entity2.getContent()));
			String buffer2;
			while((buffer2=reader2.readLine())!=null){
				System.out.println(buffer2);
			}
		}else if(statusCode == 302){//302表示重定向
			Header[] hs = response2.getHeaders("Location");
			if(hs.length>0){
				String url = hs[0].getValue();
				post.abort();
				get(client,url);
			}
		}
	}

	/**
	 * 模拟GET请求
	 * @param client
	 * @param url
	 * @throws IOException
	 * @throws ClientProtocolException
	 */
	private static void get(HttpClient client,String url) throws IOException,
			ClientProtocolException {
		System.out.println("======GET:"+url+"===========================");
		HttpGet get = new HttpGet(url);
		HttpResponse response = client.execute(get);
		HttpEntity entity = response.getEntity();
		BufferedReader reader = new BufferedReader(
				new InputStreamReader(entity.getContent(),"UTF-8"));
		String buffer = null;
		while((buffer=reader.readLine())!=null){
			System.out.println(buffer);
		}
		get.abort();
	}
}

 

你可能感兴趣的:(apache,.net,jsp,asp.net,asp)