博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
发出HTTP请求并获得HTTP响应
阅读量:5291 次
发布时间:2019-06-14

本文共 3990 字,大约阅读时间需要 13 分钟。

发出HTTP请求并获得HTTP响应的过程如下:

(1)定义HTTP请求HttpPut(HttpPost/HttpGet/HttpDelete)等;

(2)定义各种Header,并加入HttpPut中;

(3)定义HttpEntitty,将要发送的XML字符串加入HttpEntity,并将HttpEntity加入HttpPut;

(4)定义HttpClient,执行HttpPut,并返回HttpResponse;

(5)从HttpResponse中获得StatusCode;

(6)从HttpResponse中获得各种Header;

(7)从HttpResponse中获得HttpEntity,并获得其中的字符串输出。

代码如下:

// 修改队列参数    public static void modify_queue_attributes(){                // 定义HttpClient        HttpClient httpClient = new DefaultHttpClient();        // Uri        String hs = host + "/" + queName +"?Metaoverride=true";        // 定义HttpPut        HttpPut httpPut = new HttpPut(hs);        // 各种Header        httpPut.setHeader("Date", time);        System.out.println("Date:" + time);        httpPut.setHeader("x-mqs-version", version);        System.out.println("x-mqs-version:" + version);        httpPut.setHeader("Authorization", auth);        System.out.println("Authorization:" + auth);        httpPut.setHeader("Content-Type", content_type);        System.out.println("Content-Type:" + content_type);        // 消息体内容(XML格式)        StringBuilder req = new StringBuilder();        String s = new String("");        s = "
"; req.append(s); s = "
"; req.append(s); s = "
120
"; req.append(s); s = "
1024
"; req.append(s); s = "
120
"; req.append(s); s = "
60
"; req.append(s); s = "
"; req.append(s); System.out.println(); System.out.println(req); // 定义HttpEntity HttpEntity entity; try{ // StringEntity entity = new StringEntity(req.toString()); // 为HttpPut设置Entity httpPut.setEntity(entity); // HttpClient执行HttpPut,并返回HttpResponse HttpResponse httpResponse = httpClient.execute(httpPut); // 获得StatusCode int statusCode = httpResponse.getStatusLine().getStatusCode(); System.out.println("Status Code: " + statusCode); // 获得HttpResponse的各种Header Header[] header; header = httpResponse.getHeaders("Content-Length"); if (header.length != 0){ // System.out.println(header[0].toString()); } header = httpResponse.getHeaders("Connection"); if (header.length != 0){ // System.out.println(header[0].toString()); } header = httpResponse.getHeaders("Date"); if (header.length != 0){ // System.out.println(header[0].toString()); } header = httpResponse.getHeaders("Server"); if (header.length != 0){ // System.out.println(header[0].toString()); } header = httpResponse.getHeaders("x-mqs-request-id"); if (header.length != 0){ // System.out.println(header[0].toString()); } header = httpResponse.getHeaders("x-mqs-version"); if (header.length != 0){ // System.out.println(header[0].toString()); } // 获得HttpResponse的HttpEntity并以字符串形式输出 HttpEntity httpEntity = httpResponse.getEntity(); if (httpEntity != null){ InputStream instreams = httpEntity.getContent(); String str = ConvertStreamToString(instreams); System.out.println("Response:" + "\n" + str); } } catch(Exception e){ System.out.println("Error=" + e.toString()); } }

返回结果为:

Status Code: 403Response:
SignatureDoesNotMatch
The request signature we calculated does not match the signature you provided. Check your key and signing method.
53D60FE4048A936A361D8ABA
http://huvaw6yih3.mqs-cn-hangzhou.aliyuncs.com

 

转载于:https://www.cnblogs.com/mstk/p/3873497.html

你可能感兴趣的文章
Spring boot中普通工具类不能使用@Value注入yml文件中的自定义参数的问题
查看>>
[8.3] Magic Index
查看>>
(转·)WMPLib
查看>>
C语言结构体对齐
查看>>
跨应用Session共享
查看>>
Vue动态路由
查看>>
电脑小窍门
查看>>
IDEA环境设置
查看>>
Oracle行列转换小结
查看>>
W-D-S-链接地址
查看>>
3、图片处理
查看>>
HTML-日记3
查看>>
java enum 用法
查看>>
java常见文件操作
查看>>
python虚拟环境的安装和配置
查看>>
在eclipse中添加和删除项目
查看>>
Search a 2D Matrix & II
查看>>
网站更新后客户端缓存问题
查看>>
Android OpenGL ES(四)关于EGL .
查看>>
thinkphp整合系列之苹果AppStore内购付款的服务器端php验证
查看>>