HttpClient4使用-飞外

import org.apache.http.ParseException;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;import org.apache.http.util.EntityUtils;public class Main { private static HttpClient hc = new DefaultHttpClient(); * @param args public static void main(String[] args) { List NameValuePair params = new ArrayList NameValuePair params.add(new BasicNameValuePair("email", "xxx@gmail.com")); params.add(new BasicNameValuePair("pwd", "xxx")); params.add(new BasicNameValuePair("save_login", "1")); String url = "http://www.oschina.net/action/user/login"; String body = post(url, params); System.out.println(body); * Get请求 * @param url * @param params * @return public static String get(String url, List NameValuePair params) { String body = null; try { // Get请求 HttpGet httpget = new HttpGet(url); // 设置参数 String str = EntityUtils.toString(new UrlEncodedFormEntity(params)); httpget.setURI(new URI(httpget.getURI().toString() + "?" + str)); // 发送请求 HttpResponse httpresponse = hc.execute(httpget); // 获取返回数据 HttpEntity entity = httpresponse.getEntity(); body = EntityUtils.toString(entity); if (entity != null) { entity.consumeContent(); } catch (ParseException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (URISyntaxException e) { e.printStackTrace(); return body; * // Post请求 * @param url * @param params * @return public static String post(String url, List NameValuePair params) { String body = null; try { // Post请求 HttpPost httppost = new HttpPost(url); // 设置参数 httppost.setEntity(new UrlEncodedFormEntity(params)); // 发送请求 HttpResponse httpresponse = hc.execute(httppost); // 获取返回数据 HttpEntity entity = httpresponse.getEntity(); body = EntityUtils.toString(entity); if (entity != null) { entity.consumeContent(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (ParseException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); return body;


importorg.apache.http.ParseException;importorg.apache.http.client.ClientProtocolException;importorg.apache.http.client.HttpClient;importorg.apache.http.client.entity.UrlEncodedFormEntity;importorg.apache.http.client.methods.HttpGet;importorg.apache.http.client.methods.HttpPost;importorg.apache.http.impl.client.DefaultHttpClient;importorg.apache.http.message.BasicNameValuePair;importorg.apache.http.util.EntityUtils;publicclassMain {privatestaticHttpClient hc =newDefaultHttpClient();/*** @param args*/publicstaticvoidmain(String[] args) {List NameValuePair params =newArrayList NameValuePair params.add(newBasicNameValuePair("email","xxx@gmail.com"));params.add(newBasicNameValuePair("pwd","xxx"));params.add(newBasicNameValuePair("save_login","1"));String url ="http://www.oschina.net/action/user/login";String body = post(url, params);System.out.println(body);}/*** Get请求* @param url* @param params* @return*/publicstaticString get(String url, List NameValuePair params) {String body =null;try{// Get请求HttpGet httpget =newHttpGet(url);// 设置参数String str = EntityUtils.toString(newUrlEncodedFormEntity(params));httpget.setURI(newURI(httpget.getURI().toString() +"?"+ str));// 发送请求HttpResponse httpresponse = hc.execute(httpget);// 获取返回数据HttpEntity entity = httpresponse.getEntity();body = EntityUtils.toString(entity);if(entity !=null) {entity.consumeContent();}}catch(ParseException e) {e.printStackTrace();}catch(UnsupportedEncodingException e) {e.printStackTrace();}catch(IOException e) {e.printStackTrace();}catch(URISyntaxException e) {e.printStackTrace();}returnbody;}/*** // Post请求* @param url* @param params* @return*/publicstaticString post(String url, List NameValuePair params) {String body =null;try{// Post请求HttpPost httppost =newHttpPost(url);// 设置参数httppost.setEntity(newUrlEncodedFormEntity(params));// 发送请求HttpResponse httpresponse = hc.execute(httppost);// 获取返回数据HttpEntity entity = httpresponse.getEntity();body = EntityUtils.toString(entity);if(entity !=null) {entity.consumeContent();}}catch(UnsupportedEncodingException e) {e.printStackTrace();}catch(ClientProtocolException e) {e.printStackTrace();}catch(ParseException e) {e.printStackTrace();}catch(IOException e) {e.printStackTrace();}returnbody;}