java類比https請求

來源:互聯網
上載者:User
最近一個APP要用到雲端儲存,比較了幾個雲空間後,最後選擇了Bmob(http://www.bmob.cn/),Bmob功能不少,還提供各種API,免費。

由於我的APP較小,業務也簡單,所以直接用Bmob提供的Rest API。

Bmob的Rest API是https協議的,所以我的想法是:先匯出Bmob的認證,用java的keyTool工具製成認證庫(keystore),再用HttpsURLConnection與Bmob伺服器溝通。

下面為具體步驟:

 1:匯出認證
用IE的認證工具可以將網站的認證匯出。開啟網頁,查看網頁的屬性頁面,屬性頁面右下角可以找到認證,最後將認證【複製到檔案】。



由於Java的keyTool工具不能匯入P7B格式的認證,所以在匯入嚮導中,我選擇Base64編碼的CER認證:


最終得到認證檔案:


 2:建立認證庫並匯入認證
用java的keyTool工具,執行下列命令,得到bmob.keystore認證庫檔案:

--產生認證庫檔案
keytool -genkey -alias bmob.keystore -keyalg RSA -validity 100000 -keystore bmob.keystore

--匯入Der/Cer認證
keytool -import -file bmob.cer -keystore bmob.keystore

註:將bmob.cer檔案匯入認證庫時,最好與認證庫同一目錄

 3:java調用請求
靜態塊設定認證:
/** * 載入認證 * */static {System.setProperty("javax.net.ssl.trustStore", "C:\\bmob.keystore");System.setProperty("javax.net.ssl.trustStorePassword", "password");}

POST方式:
/** * 添加例子 * @see <a href='http://docs.bmob.cn/restful/developdoc/index.html?menukey=develop_doc&key=develop_restful#index_添加資料'>例子</a> * */public static void add() throws Exception {//構建請求URL postUrl = new URL(ADD_URL);HttpsURLConnection con = (HttpsURLConnection) postUrl.openConnection();//開啟串連 con.setRequestMethod("POST");//post方式提交con.setDoOutput(true);//開啟讀寫屬性,預設均為false con.setDoInput(true);con.setUseCaches(false);//Post請求不能使用緩衝 con.setInstanceFollowRedirects(true);//添加頭資訊con.setRequestProperty("X-Bmob-Application-Id", APP_ID);con.setRequestProperty("X-Bmob-REST-API-Key", API_Key);con.setRequestProperty("Content-Type", "application/json");DataOutputStream out = new DataOutputStream(con.getOutputStream());//發送請求String data = "{\"name\":\"tom\"}";out.writeBytes(data);out.flush();out.close();//接收資料BufferedReader reader = new BufferedReader(new InputStreamReader(con.getInputStream(), "UTF-8"));String line;StringBuffer responseText = new StringBuffer();while ((line = reader.readLine()) != null) {responseText.append(line).append("\r\n");}reader.close();con.disconnect();System.out.println(responseText.toString());}

GET方式:
/** * 查詢資料例子 * @see <a target=_blank href="http://docs.bmob.cn/restful/developdoc/index.html?menukey=develop_doc&key=develop_restful#index_查詢資料">例子</a> * */public static void select() throws Exception {//構建請求URL postUrl = new URL(SELECT_URL);HttpsURLConnection con = (HttpsURLConnection) postUrl.openConnection();//開啟串連 con.setRequestMethod("GET");//get方式提交con.setDoInput(true);con.setInstanceFollowRedirects(true);//添加頭資訊con.setRequestProperty("X-Bmob-Application-Id", APP_ID);con.setRequestProperty("X-Bmob-REST-API-Key", API_Key);con.setRequestProperty("Content-Type", "application/json");//接收資料BufferedReader reader = new BufferedReader(new InputStreamReader(con.getInputStream(), "UTF-8"));String line;StringBuffer responseText = new StringBuffer();while ((line = reader.readLine()) != null) {responseText.append(line).append("\r\n");}reader.close();con.disconnect();System.out.println(responseText.toString());}

其實與http請求差不多,只是多了認證製作部分。

聯繫我們

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