HttpClient模拟请求实例
测试类
package com.chen.test;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import com.chen.httpclient.MyHttpClient;
public class MyTest {
public static void main(String[] args) throws UnsupportedEncodingException {
String result;//返回结果
List<NameValuePair> nvpList=new ArrayList<NameValuePair>();//存放参数
NameValuePair pair1;//参数
pair1 = new BasicNameValuePair("mytext","漫画");
nvpList.add(pair1);//添加参数
String url="http://localhost:8080/MyAjax/form_submit";//指定URL
try {
result=MyHttpClient.send(url, nvpList);//执行函数返回结果
System.out.println(result);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
/*try {
result=MyHttpClient.read("http://localhost:8080/MyAjax/form_submit.jsp");
System.out.println(result);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}*/
}
}
核心代码
package com.chen.httpclient;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.List;
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.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
/**
*
* @author CHEN
*
*/
public class MyHttpClient {
private static HttpClient httpClient = new DefaultHttpClient();
public static String send(String url, List<NameValuePair> nvpList) throws ClientProtocolException, IOException {
HttpResponse response = null;
HttpEntity entity = null;
HttpPost post = null;
entity = new UrlEncodedFormEntity(nvpList,"UTF-8");//设置参数对象并设定字符格式UTF-8
//配置
post = new HttpPost(url);// 装上路径
post.setEntity(entity);// 装上物品
//执行
response = httpClient.execute(post);
//执行结果
entity=response.getEntity();
//IO读结果
BufferedReader reader=new BufferedReader(new InputStreamReader(entity.getContent(),"UTF-8"));
StringBuffer result=new StringBuffer();
String tempLine=reader.readLine();
while(tempLine!=null) {
result.append(tempLine);
tempLine=reader.readLine();
}
reader.close();
return result.toString();
}
public static void get(String url,List<NameValuePair> nvpList) {
//HttpGet
}
public static String read(String url) throws IOException {
URL myUrl=new URL(url);
DataInputStream diStream=new DataInputStream(myUrl.openStream());
StringBuffer result=new StringBuffer();
String temp;
while((temp=diStream.readLine())!=null) {
result.append(temp);
}
return result.toString();
}
}
简介HttpClient
HttpClient是一个客户端http通信实现库,他的目的是发送和接收http报文,他不会缓存数据,不会执行html页面中的js,不会猜测内容类型,不会格式化,不会重定向,亦不会一切与http传输无关的东西。
优点
1. 以可扩展面向等于对象的结构实现了Http的全部方法(GET/POST/PUT/DELETE/HEAD/OPTION/TRACE)
2. 支持https协议
3. 使用connect方法通过http代理建立隧道的https链接
4. 插件似的自定义认证方案
5. 自动处理set-cookie中的cookie