天天看點

HTTP接口GET方式調用執行個體

public void xiaounsubscribe(){

String sign=MD5Tool.md5Encryption("unifiedOrderId=8101011224917708589&key=G0P");//MD5加密

String result = "";

BufferedReader in = null;

try {

String urlName = "http://ip:端口/orderSd/cribe?OrderId=8101011224917708589&sign="+sign;//接口位址,後面跟參數用?隔開,參數之間用&分隔。

URL realUrl = new URL(urlName);

//打開和URL之間的連接配接

URLConnection conn = realUrl.openConnection();

//設定通用的請求屬性

conn.setRequestProperty("accept", "*/*");

conn.setRequestProperty("connection", "Keep-Alive");

conn.setRequestProperty("user-agent",

"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");

//建立實際的連接配接

conn.connect();

//擷取所有響應頭字段

Map < String, List < String >> map = conn.getHeaderFields();

//周遊所有的響應頭字段

for (String key: map.keySet()) {

System.out.println(key + "--->" + map.get(key));

}

//定義BufferedReader輸入流來讀取URL的響應

in = new BufferedReader(new InputStreamReader(conn.getInputStream()));

String line;

while ((line = in .readLine()) != null) {

result +=line;

}

System.out.println("退訂傳回結果======================"+result.toString());

} catch (Exception e) {

System.out.println("發送GET請求出現異常!" + e);

e.printStackTrace();

}

//使用finally塊來關閉輸入流

finally {

try {

if ( in != null) { in .close();

}

} catch (IOException ex) {

ex.printStackTrace();

}

}

}

繼續閱讀