package demo.httpclient; |
002 |
003 | import java.io.BufferedReader; |
004 | import java.io.IOException; |
005 | import java.io.InputStream; |
006 | import java.io.InputStreamReader; |
007 | import java.util.ArrayList; |
008 | import java.util.List; |
009 |
010 | import org.apache.http.HeaderIterator; |
011 | import org.apache.http.HttpEntity; |
012 | import org.apache.http.HttpStatus; |
013 | import org.apache.http.NameValuePair; |
014 | import org.apache.http.client.ClientProtocolException; |
015 | import org.apache.http.client.config.CookieSpecs; |
016 | import org.apache.http.client.config.RequestConfig; |
017 | import org.apache.http.client.entity.UrlEncodedFormEntity; |
018 | import org.apache.http.client.methods.CloseableHttpResponse; |
019 | import org.apache.http.client.methods.HttpGet; |
020 | import org.apache.http.client.methods.HttpPost; |
021 | import org.apache.http.impl.client.CloseableHttpClient; |
022 | import org.apache.http.impl.client.HttpClients; |
023 | import org.apache.http.message.BasicNameValuePair; |
024 | import org.apache.http.util.EntityUtils; |
025 |
026 | public class HttpClientDemo { |
027 | public static void main(String[] args) { |
028 | CloseableHttpClient httpclient = HttpClients.createDefault(); |
029 | HttpGet httpget = new HttpGet( "http://www.soso.com" ); |
030 | CloseableHttpResponse response = null ; |
031 | |
032 | // 设置请求和传输超时时间5s,设置cookie策略 |
033 | RequestConfig requestconfig = RequestConfig.custom().setSocketTimeout( 5000 ) |
034 | .setConnectTimeout( 5000 ).setCookieSpec(CookieSpecs.BEST_MATCH).build(); |
035 | httpget.setConfig(requestconfig); |
036 | |
037 | System.out.println(httpget.getURI()); |
038 | // 打印请求信息 |
039 | System.out.println( "Executing request " + httpget.getRequestLine()); |
040 | System.out.println( "------------------------------" ); |
041 | |
042 | try { |
043 | response = httpclient.execute(httpget); |
044 | |
045 | System.out.println(response.getStatusLine().toString()); |
046 | System.out.println( "------------------------------" ); |
047 | |
048 | // 头信息 |
049 | HeaderIterator it = response.headerIterator(); |
050 | while (it.hasNext()) { |
051 | System.out.println(it.next()); |
052 | } |
053 | System.out.println( "------------------------------" ); |
054 | |
055 | // 判断访问的状态码 |
056 | int statusCode = response.getStatusLine().getStatusCode(); |
057 | if (statusCode != HttpStatus.SC_OK) { |
058 | System.err.println( "Method failed: " + response.getStatusLine()); |
059 | } |
060 | // if(statusCode == HttpStatus.SC_OK) { |
061 | // |
062 | // } else if((statusCode == HttpStatus.SC_MOVED_TEMPORARILY) |
063 | // || (statusCode == HttpStatus.SC_MOVED_PERMANENTLY) |
064 | // || (statusCode == HttpStatus.SC_SEE_OTHER) |
065 | // || (statusCode == HttpStatus.SC_TEMPORARY_REDIRECT)) { |
066 | // //页面重定向代码 |
067 | // } |
068 | |
069 | HttpEntity entity = response.getEntity(); |
070 | StringBuilder pageBuffer = new StringBuilder(); |
071 | if (entity != null ) { |
072 | InputStream in = entity.getContent(); |
073 | BufferedReader br = new BufferedReader( new InputStreamReader(in, "utf-8" )); |
074 | String line; |
075 | while ((line = br.readLine()) != null ) { |
076 | pageBuffer.append(line); |
077 | pageBuffer.append( "\n" ); |
078 | } |
079 | System.out.println(pageBuffer.toString()); |
080 | in.close(); |
081 | br.close(); |
082 | } |
083 | |
084 | System.out.println( "------------------------------" ); |
085 | |
086 | HttpPost httpPost = new HttpPost( "http://www.weibo.com" ); |
087 | |
088 | // 将要POST的数据封包 |
089 | List<NameValuePair> nvps = new ArrayList<NameValuePair>(); |
090 | nvps.add( new BasicNameValuePair( "username" , "vip" )); |
091 | nvps.add( new BasicNameValuePair( "password" , "123456" )); |
092 | httpPost.setEntity( new UrlEncodedFormEntity(nvps, "utf-8" )); |
093 | |
094 | CloseableHttpResponse response1 = httpclient.execute(httpPost); |
095 | try { |
096 | HttpEntity entity1 = response1.getEntity(); |
097 | if (entity != null ) { |
098 | System.out.println(EntityUtils.toString(entity1, "UTF-8" )); |
099 | } |
100 | EntityUtils.consume(entity1); |
101 | } finally { |
102 | response.close(); |
103 | } |
104 | |
105 | } catch (ClientProtocolException e) { |
106 | e.printStackTrace(); |
107 | } catch (IOException e) { |
108 | e.printStackTrace(); |
109 | } finally { |
110 | try { |
111 | response.close(); |
112 | httpclient.close(); |
113 | } catch (IOException e) { |
114 | e.printStackTrace(); |
115 | } |
116 | } |
117 | } |
118 | } |