採用http請求第三方介面實現Demo

來源:互聯網
上載者:User

標籤:通訊   http   第三方介面   

    我們在調用第三方介面的時候,有的時候需要採用http協議進行通訊,這裡簡單介紹一下http請求的執行過程。

  寫在前面:

你需要找到一個能訪問的介面url,知道需要傳遞什麼參數,請求的編碼格式。下面是我寫的一個樣本。
package cn.sccl.lijianbo;import java.io.IOException;import java.util.ArrayList;import java.util.List;import org.apache.http.HttpEntity;import org.apache.http.HttpResponse;import org.apache.http.NameValuePair;import org.apache.http.client.ClientProtocolException;import org.apache.http.client.entity.UrlEncodedFormEntity;import org.apache.http.client.methods.HttpPost;import org.apache.http.impl.client.DefaultHttpClient;import org.apache.http.message.BasicNameValuePair;import org.apache.http.protocol.HTTP;import org.apache.http.util.EntityUtils;import org.junit.Test;/** * 測試http的訪問需要的條件 * http執行的流程 * @author lijianbo * 2016年11月19日11:28:12 * */public class HttpFlow2 {@Testpublic void testHttpFlow() {try {/* * 介面url */String url = "https://www.baidu.com/";//需要自己修改/* * 發送請求 */// 建立HttpPost對象HttpPost httpPost = new HttpPost(url);// 建立一個NameValuePair數組,用於儲存欲傳送的參數List<NameValuePair> params = new ArrayList<NameValuePair>();/* *  添加參數 */ params.add(new BasicNameValuePair("name","小明")); params.add(new BasicNameValuePair("password","123456"));// 佈建要求參數編碼httpPost.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));/* * 佈建要求頭,好像這裡也可以不用處理 *//*if (httpParameter2 != null && !httpParameter2.isEmptyHeader()) {Set<Entry<String, String>> set = httpParameter2.getHeaderMap().entrySet();for (Entry<String, String> e : set) {httpPost.addHeader(e.getKey(), e.getValue());}}*/// 發送Post,並返回一個HttpResponse對象HttpResponse response = new DefaultHttpClient().execute(httpPost);/* * 處理傳回值 */int statusCode = response.getStatusLine().getStatusCode();HttpEntity entity = response.getEntity();//設定返回內容編碼,並轉為string,也可以轉換為其他類型String entityStr = EntityUtils.toString(entity, "UTF-8");System.out.println("狀態代碼為===" + statusCode);System.out.println("返回內容為===" + entityStr);} catch (ClientProtocolException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}}}/*說明: * List<NameValuePair> params = new ArrayList<NameValuePair>();定義了一個list,該list的資料類型是NameValuePair(簡單名稱值對節點類型),這個代碼多處用於Java像url發送Post請求。在發送post請求時用該list來存放參數。*/

輸出結果:

狀態代碼為===200

返回內容為==={"status":102,"msg":"phone has been registed\uff01"


寫在後面:


http請求的返回有多種形式,json、xml、HTML、text等,這個要根據介面文檔來看,介面返回什麼是寫介面的人已經確定了的。

http協議的常用術語:

。。。。。

--------------------------------------------------------------

歡迎你的閱讀,如有描述不對之處請留言。我是Jon,讓我們一起成長。

最新更新:2016年11月19日12:10:26

--------------------------------------------------------------

採用http請求第三方介面實現Demo

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.