`
wlei1818
  • 浏览: 68881 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

HttpClient入门及模拟浏览器实现Get、Post

 
阅读更多
HttpClientTest.java
public class HttpClientTest {

	public static void main(String[] args) throws ClientProtocolException, IOException {

		HttpClient client = new DefaultHttpClient();
		
		try{
			HttpPost post = new HttpPost("http://localhost:8080/sevletTest/page/success.jsp");
			
			List<NameValuePair> nvps = new ArrayList<NameValuePair>();
			nvps.add(new BasicNameValuePair("username", "Tom"));
			nvps.add(new BasicNameValuePair("password", "12345"));
			HttpEntity entity = new UrlEncodedFormEntity(nvps, "UTF-8");
			
			post.setEntity(entity);
			
			System.out.println("Executing Request: " + post.getURI());
			
			HttpResponse response = client.execute(post);
			entity = response.getEntity();
			System.out.println("Response Status: " + response.getStatusLine());
			if(entity != null){
				System.out.println("Response Content: " + EntityUtils.toString(entity, "UTF-8"));
			}
			
		}finally{
			client.getConnectionManager().shutdown();
		}
		
	}

}



HttpClient 学习整理
应用HttpClient来对付各种顽固的WEB服务器
HttpClient_4 用法 由HttpClient_3 升级到 HttpClient_4 必看
HttpClient
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics