需要导入的jar包
HttpClient4使用
001 | package main; |
002 |
003 | import java.io.IOException; |
004 | import java.io.UnsupportedEncodingException; |
005 | import java.net.URI; |
006 | import java.net.URISyntaxException; |
007 | import java.util.ArrayList; |
008 | import java.util.List; |
009 |
010 | import org.apache.http.HttpEntity; |
011 | import org.apache.http.HttpResponse; |
012 | import org.apache.http.NameValuePair; |
013 | import org.apache.http.ParseException; |
014 | import org.apache.http.client.ClientProtocolException; |
015 | import org.apache.http.client.HttpClient; |
016 | import org.apache.http.client.entity.UrlEncodedFormEntity; |
017 | import org.apache.http.client.methods.HttpGet; |
018 | import org.apache.http.client.methods.HttpPost; |
019 | import org.apache.http.impl.client.DefaultHttpClient; |
020 | import org.apache.http.message.BasicNameValuePair; |
021 | import org.apache.http.util.EntityUtils; |
022 |
023 | public class Main { |
024 |
025 |
private static HttpClient hc = new DefaultHttpClient(); |
026 |
027 |
/** |
028 |
* @param args |
029 |
*/ |
030 |
public static void main(String[] args) { |
031 |
List<NameValuePair> params = new ArrayList<NameValuePair>(); |
032 |
params.add( new BasicNameValuePair( "email" , "xxx@gmail.com" )); |
033 |
params.add( new BasicNameValuePair( "pwd" , "xxx" )); |
034 |
params.add( new BasicNameValuePair( "save_login" , "1" )); |
035 |
036 |
String url = "http://www.oschina.net/action/user/login" ; |
037 |
038 |
String body = post(url, params); |
039 |
System.out.println(body); |
040 |
} |
041 |
042 |
/** |
043 |
* Get请求 |
044 |
* @param url |
045 |
* @param params |
046 |
* @return |
047 |
*/ |
048 |
public static String get(String url, List<NameValuePair> params) { |
049 |
String body = null ; |
050 |
try { |
051 |
// Get请求 |
052 |
HttpGet httpget = new HttpGet(url); |
053 |
// 设置参数 |
054 |
String str = EntityUtils.toString( new UrlEncodedFormEntity(params)); |
055 |
httpget.setURI( new URI(httpget.getURI().toString() + "?" + str)); |
056 |
// 发送请求 |
057 |
HttpResponse httpresponse = hc.execute(httpget); |
058 |
// 获取返回数据 |
059 |
HttpEntity entity = httpresponse.getEntity(); |
060 |
body = EntityUtils.toString(entity); |
061 |
if (entity != null ) { |
062 |
entity.consumeContent(); |
063 |
} |
064 |
} catch (ParseException e) { |
065 |
e.printStackTrace(); |
066 |
} catch (UnsupportedEncodingException e) { |
067 |
e.printStackTrace(); |
068 |
} catch (IOException e) { |
069 |
e.printStackTrace(); |
070 |
} catch (URISyntaxException e) { |
071 |
e.printStackTrace(); |
072 |
} |
073 |
return body; |
074 |
} |
075 |
076 |
/** |
077 |
* // Post请求 |
078 |
* @param url |
079 |
* @param params |
080 |
* @return |
081 |
*/ |
082 |
public static String post(String url, List<NameValuePair> params) { |
083 |
String body = null ; |
084 |
try { |
085 |
// Post请求 |
086 |
HttpPost httppost = new HttpPost(url); |
087 |
// 设置参数 |
088 |
httppost.setEntity( new UrlEncodedFormEntity(params)); |
089 |
// 发送请求 |
090 |
HttpResponse httpresponse = hc.execute(httppost); |
091 |
// 获取返回数据 |
092 |
HttpEntity entity = httpresponse.getEntity(); |
093 |
body = EntityUtils.toString(entity); |
094 |
if (entity != null ) { |
095 |
entity.consumeContent(); |
096 |
} |
097 |
} catch (UnsupportedEncodingException e) { |
098 |
e.printStackTrace(); |
099 |
} catch (ClientProtocolException e) { |
100 |
e.printStackTrace(); |
101 |
} catch (ParseException e) { |
102 |
e.printStackTrace(); |
103 |
} catch (IOException e) { |
104 |
e.printStackTrace(); |
105 |
} |
106 |
return body; |
107 |
} |
108 |
109 | } |