HTTP接口GET方式调用实例

/**

* @Title: xiaounsubscribe

* @author:malz

* @date: 2018-10-10下午3:18:14

* @Description:接口测试

*/

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();

}

}

}

猜你喜欢

转载自blog.csdn.net/malz_zh/article/details/83339600