需要导入的jar包
HttpClient4使用
001 | packagemain; |
002 |
003 | importjava.io.IOException; |
004 | importjava.io.UnsupportedEncodingException; |
005 | importjava.net.URI; |
006 | importjava.net.URISyntaxException; |
007 | importjava.util.ArrayList; |
008 | importjava.util.List; |
009 |
010 | importorg.apache.http.HttpEntity; |
011 | importorg.apache.http.HttpResponse; |
012 | importorg.apache.http.NameValuePair; |
013 | importorg.apache.http.ParseException; |
014 | importorg.apache.http.client.ClientProtocolException; |
015 | importorg.apache.http.client.HttpClient; |
016 | importorg.apache.http.client.entity.UrlEncodedFormEntity; |
017 | importorg.apache.http.client.methods.HttpGet; |
018 | importorg.apache.http.client.methods.HttpPost; |
019 | importorg.apache.http.impl.client.DefaultHttpClient; |
020 | importorg.apache.http.message.BasicNameValuePair; |
021 | importorg.apache.http.util.EntityUtils; |
022 |
023 | publicclassMain { |
024 |
025 | privatestaticHttpClient hc =newDefaultHttpClient(); |
026 |
027 | /** |
028 | * @param args |
029 | */ |
030 | publicstaticvoidmain(String[] args) { |
031 | List<NameValuePair> params =newArrayList<NameValuePair>(); |
032 | params.add(newBasicNameValuePair("email","xxx@gmail.com")); |
033 | params.add(newBasicNameValuePair("pwd","xxx")); |
034 | params.add(newBasicNameValuePair("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 | publicstaticString get(String url, List<NameValuePair> params) { |
049 | String body =null; |
050 | try{ |
051 | // Get请求 |
052 | HttpGet httpget =newHttpGet(url); |
053 | // 设置参数 |
054 | String str = EntityUtils.toString(newUrlEncodedFormEntity(params)); |
055 | httpget.setURI(newURI(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 | returnbody; |
074 | } |
075 |
076 | /** |
077 | * // Post请求 |
078 | * @param url |
079 | * @param params |
080 | * @return |
081 | */ |
082 | publicstaticString post(String url, List<NameValuePair> params) { |
083 | String body =null; |
084 | try{ |
085 | // Post请求 |
086 | HttpPost httppost =newHttpPost(url); |
087 | // 设置参数 |
088 | httppost.setEntity(newUrlEncodedFormEntity(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 | returnbody; |
107 | } |
108 |
109 | } |
使用Apache HttpClient4进行HTTP请求
983

被折叠的 条评论
为什么被折叠?



