httpclient 4.0 使用

來源:互聯網
上載者:User

標籤:apach   query   下一步   釋放   pair   pen   puts   風格   nbsp   

httclient 正常流程

1、建立httpclient執行個體

HttpClient httpClient = new DefaultHttpClient();CloseableHttpClient httpclient =  HttpClients.createDefault();

 

2、建立請求執行個體

HttpGet httpGet = new HttpGet(url);HttpPost httpPost = new HttpPost(url);

 

3、補充頭資訊(可選)

httpPost.addHeader("Referer", "http://iservice.10010.com/e4/query/basic/history_list.html");httpPost.addHeader(" Cookie ", " td_cookie=18446744072103645798; mallcity=31|310; ");

 

4、補充請求實體

1)url是restful風格,已經包含需要傳入的參數。可以跳過直接到下一步操作。

url="https://ssoqa.99bill.com/sso/login/smsvalidate.htm?method=loginErrorCount&idContent=1234567" 

 

2)表單

List<NameValuePair> nvps = new ArrayList<NameValuePair>();nvps.add(new BasicNameValuePair("querytype", "0001"));nvps.add(new BasicNameValuePair("querycode", "0001"));nvps.add(new BasicNameValuePair("billdate", "201608"));nvps.add(new BasicNameValuePair("flag", "2"));httpPost.setEntity(new UrlEncodedFormEntity(nvps));

 

5、請求執行

HttpResponse response = httpClient.execute(httpGet);CloseableHttpResponse response = httpclient.execute(httpGet);byte[] response = httpClient.execute(httpGet,handler);

 

6、讀取響應內容

if (response.getStatusLine().getStatusCode() == 200) {    HttpEntity entity = response.getEntity();     if (entity != null) {                     InputStream instream = entity.getContent();            try {           // do something useful             } finally {                instream.close();            }        }   } 

 

7、釋放串連

response.close(); 

 

 

需要引用的jar

        <!-- httpclient -->        <dependency>            <groupId>org.apache.httpcomponents</groupId>            <artifactId>httpclient</artifactId>            <version>4.3.4</version>        </dependency>        <!-- httpclient -->

 

httpclient 4.0 使用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.