HTTP 请求

前言

我用是apache-httpclient这个jar包,依赖坐标

<dependency>
     <groupId>apache-httpclient</groupId>
     <artifactId>commons-httpclient</artifactId>
     <version>3.1</version>
 </dependency>
1、http put

功能:上传一个图片,请求头参数是签名及contentType,文件流放到请求体里面
请求格式:

PUT /fileName HTTP/1.1
Host: test.clockbone.com
signature: <signature>
Content-Length: <length>
Content-Type: <mimetype>

[11434 bytes of file data]

返回格式:

HTTP/1.1 200 OK
Content-Length: 0
body: fileName

代码:

public String httpPut() throws FileNotFoundException {
     String privateKey = "privateKey";

     //请求路径
     String fileName= "1.png";
     String url = "http://test.clockbone.com"+"/"+fileName;

     String method = "PUT";
     String contentType = "image/jpeg";
     String date = System.currentTimeMillis()+"";

     String sign = method + "\n" +
             contentType + "\n" +
             date;
     //用HmacSHA1加密,并base64输出
     String signature   = genHMAC(sign,privateKey);
     System.out.println("authorization=======================");
     System.out.println(signature);

     HttpPut httpPut = new HttpPut(url);
     httpPut.setHeader("signature",signature);
     httpPut.setHeader("Content-Type",contentType);
     String tmpFilePath = "";
     tmpFilePath = "D:\\data\\1.png";
     File file = new File(tmpFilePath);
     InputStream in = new FileInputStream(file);
     //上传图片
     InputStreamEntity httpEntity = new InputStreamEntity(in);
     httpPut.setEntity(httpEntity);
     HttpClient httpclient = HttpClients.createDefault();
     try {
         HttpResponse response = httpclient.execute(httpPut);
         //返回状态码
         return response.getStatusLine().getStatusCode()+"";
     } catch (IOException e) {
         e.printStackTrace();
     }
     return "error";
 }

private static final String HMAC_SHA1_ALGORITHM = "HmacSHA1";
public static String genHMAC(String data, String key) {
   byte[] result = null;
   try {
       //根据给定的字节数组构造一个密钥,第二参数指定一个密钥算法的名称
       SecretKeySpec signinKey = new SecretKeySpec(key.getBytes("UTF-8"), HMAC_SHA1_ALGORITHM);
       //生成一个指定 Mac 算法 的 Mac 对象
       Mac mac = Mac.getInstance(HMAC_SHA1_ALGORITHM);
       //用给定密钥初始化 Mac 对象
       mac.init(signinKey);
       //完成 Mac 操作
       byte[] rawHmac = mac.doFinal(data.getBytes("UTF-8"));
       result = Base64.encodeBase64(rawHmac);

   } catch (NoSuchAlgorithmException e) {
       System.err.println(e.getMessage());
   } catch (InvalidKeyException e) {
       System.err.println(e.getMessage());
   } catch (UnsupportedEncodingException e) {
       e.printStackTrace();
   }
   if (null != result) {
       return new String(result);
   } else {
       return null;
   }
}
1.1 请求报文
18:26:06.917 [main] DEBUG org.apache.http.headers - http-outgoing-0 >> PUT /1.png HTTP/1.1
18:26:06.917 [main] DEBUG org.apache.http.headers - http-outgoing-0 >> signature: bc6plYaLh5gNkGI56E94CLCZxxY=
18:26:06.917 [main] DEBUG org.apache.http.headers - http-outgoing-0 >> Content-Type: image/jpeg
18:26:06.917 [main] DEBUG org.apache.http.headers - http-outgoing-0 >> Transfer-Encoding: chunked
18:26:06.917 [main] DEBUG org.apache.http.headers - http-outgoing-0 >> Host: test.clockbone.com
18:26:06.917 [main] DEBUG org.apache.http.headers - http-outgoing-0 >> Connection: Keep-Alive
18:26:06.917 [main] DEBUG org.apache.http.headers - http-outgoing-0 >> User-Agent: Apache-HttpClient/4.5.5 (Java/1.8.0_144)
18:26:06.917 [main] DEBUG org.apache.http.headers - http-outgoing-0 >> Accept-Encoding: gzip,deflate
18:26:06.917 [main] DEBUG org.apache.http.wire - http-outgoing-0 >> "PUT /1.png HTTP/1.1[\r][\n]"
18:26:06.917 [main] DEBUG org.apache.http.wire - http-outgoing-0 >> "signature: bc6plYaLh5gNkGI56E94CLCZxxY=[\r][\n]"
18:26:06.917 [main] DEBUG org.apache.http.wire - http-outgoing-0 >> "Content-Type: image/jpeg[\r][\n]"
18:26:06.917 [main] DEBUG org.apache.http.wire - http-outgoing-0 >> "Transfer-Encoding: chunked[\r][\n]"
18:26:06.917 [main] DEBUG org.apache.http.wire - http-outgoing-0 >> "Host: test.clockbone.com[\r][\n]"
18:26:06.917 [main] DEBUG org.apache.http.wire - http-outgoing-0 >> "Connection: Keep-Alive[\r][\n]"
18:26:06.917 [main] DEBUG org.apache.http.wire - http-outgoing-0 >> "User-Agent: Apache-HttpClient/4.5.5 (Java/1.8.0_144)[\r][\n]"
18:26:06.917 [main] DEBUG org.apache.http.wire - http-outgoing-0 >> "Accept-Encoding: gzip,deflate[\r][\n]"
18:26:06.917 [main] DEBUG org.apache.http.wire - http-outgoing-0 >> "[\r][\n]"
18:26:06.917 [main] DEBUG org.apache.http.wire - http-outgoing-0 >> "fe[\r][\n]"
18:26:06.917 [main] DEBUG org.apache.http.wire - http-outgoing-0 >> "[0x89]PNG[\r][\n]"
18:26:06.917 [main] DEBUG org.apache.http.wire - http-outgoing-0 >> "[0x1a][\n]"
18:26:06.917 [main] DEBUG org.apache.http.wire - http-outgoing-0 >> "[0x0][0x0][0x0][\r]IHDR[0x0][0x0][0x0][0x1a][0x0][0x0][0x0][\r][0x8][0x2][0x0][0x0][0x0]8[0xfb][0xe9][0xba][0x0][0x0][0x0][0xc5]IDAT8[0x11]c[0xfc][0xf9][0xf3]'[0x3][0x6][0xf8][0xff][0xea][0x15]ci[0xe9]''[0x86][0x80][0x0]F~~[0xc]y[0x9c][0x2],Xd[0xb6]la[0x9a]?[0xff]W[0x17][0xa3][0xb2]2[0x16]Y[0xbc]B[0x18][0xc6][0xd5][0xd7]3pr[0xfe]_[0xbb][0x16][0xaf].[0x9c][0x92][0xa8][0xc6][0xd5][0xd6][0xfe][0xb7][0xb2]b[0xf4][0xf4][0xc4][0xa9][0x9c][0x90][0x4][0xc2]8[0xc6][0x88][0x88][0xbf][0xed][0xed]L[0x8a][0x8a][0x84][0xb4][0xe0][0x93]g[0x82]J[0xc6][0xc7][0xff]om[0xa5][0xd0],[0xa0]Q [0xe3][0x18][0xd3][0xd3][0xff][0x97][0x97]3[0x90][0x1e][0xf0][0x98][0xee]d[0xae][0xe5][0xe0][0xf8][0x1f][0x1c][0xcc][0xa8][0xaf][0x8f])G[0x86][0x8][0x13][0xc3][0x97]/[0x8c]&&d[0xe8][0xc4][0xaa][0x85][0xf1][0xc7][0xc7][0x8f][0x8c][0x1c][0x1c]X[0xe5][0xc8][0x10]d[0xa2][0xa2]Y@[0xeb]a1K[0x86]K[0xb0]i![0xca][0xb8][0x97]+[0xc3][0xc3]W[0xbe][0xc4][0xa6][0x9d][0x81][0xe1][0xe9][0x8a][0xf0][0xa8][0x15]p9[0x0][0x8b]`-[0x4][0x87][0x1b]K[0xa0][0x0][0x0][0x0][0x0]IEND[0xae]B`[0x82][\r][\n]"
18:26:06.917 [main] DEBUG org.apache.http.wire - http-outgoing-0 >> "0[\r][\n]"
18:26:06.917 [main] DEBUG org.apache.http.wire - http-outgoing-0 >> "[\r][\n]"
1.1 返回报文
18:26:06.949 [main] DEBUG org.apache.http.headers - http-outgoing-0 << HTTP/1.1 200 OK
18:26:06.949 [main] DEBUG org.apache.http.headers - http-outgoing-0 << Server: nginx/1.11.1
18:26:06.949 [main] DEBUG org.apache.http.headers - http-outgoing-0 << Date: Fri, 26 Oct 2018 10:26:03 GMT
18:26:06.949 [main] DEBUG org.apache.http.headers - http-outgoing-0 << Content-Type: text/plain; charset=utf-8
18:26:06.949 [main] DEBUG org.apache.http.headers - http-outgoing-0 << Content-Length: 0
18:26:06.949 [main] DEBUG org.apache.http.headers - http-outgoing-0 << Connection: keep-alive
18:26:06.949 [main] DEBUG org.apache.http.headers - http-outgoing-0 << body: "1.png"
18:26:06.949 [main] DEBUG org.apache.http.headers - http-outgoing-0 << Accept-Ranges: bytes
2 http post

请求格式:

POST / HTTP/1.1
Host: clockbone.com
Content-Type: multipart/form-data; boundary=----POSTFormBoundary

------POSTFormBoundary
Content-Disposition: form-data; name="FileName"

demofile
------POSTFormBoundary
Content-Disposition: form-data; name="signature"

asdfsad/234234fff=
------POSTFormBoundary
Content-Disposition: form-data; name="file"; filename="MyFilename.jpg"
Content-Type: image/jpeg

<file_content>
------POSTFormBoundary--

代码:

public  static String postFile1() throws FileNotFoundException {

        String privateKey = "private";

        String fileName = "1.png";
        String url = "http://test.clockbone.com"+"/"+fileName;

        String method = "POST";
        String contentType = "image/jpeg";
        String date = "";

        String stringToSign = method + "\n" +
                contentType + "\n" +
                date  ;

        String signature   = genHMAC(stringToSign,privateKey);

        String filePath = "D:\\data\\1.png";
        HttpEntity resEntity = null;
        try {
            File file = new File(filePath);

            HttpPost postMethod = new HttpPost(url);
            ContentType requestContentType = ContentType.IMAGE_JPEG;
            FileBody fileBody = new FileBody(file, requestContentType, file.getName());
            ContentType textContentType = ContentType.create("text/plain", Charset.forName("UTF-8"));

           //MultipartEntityBuilder  的请求头contentType 默认就是multipart/form-data;
            HttpEntity entity = MultipartEntityBuilder.create()
                    .setMode(HttpMultipartMode.BROWSER_COMPATIBLE)
                    .addPart("signature", new StringBody(signature, textContentType))
                    .addPart("FileName", new StringBody(fileName, textContentType))
                    .addPart("file", fileBody)
                    .build();

            postMethod.setEntity(entity);
            postMethod.setHeader(entity.getContentType());
            HttpResponse httpResponse = new DefaultHttpClient().execute(postMethod);
            resEntity = httpResponse.getEntity();
            return httpResponse.getStatusLine().getStatusCode()+"";
        } catch (Exception e) {
            e.printStackTrace();
            try {
                if (resEntity != null && resEntity.getContent() != null) {
                    resEntity.getContent().close();
                }
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }
        return "";


    }

请求报文:

19:05:00.747 [main] DEBUG org.apache.http.wire -  >> "POST /1.png HTTP/1.1[\r][\n]"
19:05:00.747 [main] DEBUG org.apache.http.wire -  >> "Content-Type: multipart/form-data; boundary=ujzwtsWv5RE2USO6SmcLMpm6WGFWReXRCzOdDjm[\r][\n]"
19:05:00.747 [main] DEBUG org.apache.http.wire -  >> "Content-Length: 733[\r][\n]"
19:05:00.747 [main] DEBUG org.apache.http.wire -  >> "Host: clockbone.com[\r][\n]"
19:05:00.747 [main] DEBUG org.apache.http.wire -  >> "Connection: Keep-Alive[\r][\n]"
19:05:00.763 [main] DEBUG org.apache.http.wire -  >> "User-Agent: Apache-HttpClient/4.5.5 (Java/1.8.0_144)[\r][\n]"
19:05:00.763 [main] DEBUG org.apache.http.wire -  >> "[\r][\n]"
19:05:00.763 [main] DEBUG org.apache.http.headers - >> POST /1.png HTTP/1.1
19:05:00.763 [main] DEBUG org.apache.http.headers - >> Content-Type: multipart/form-data; boundary=ujzwtsWv5RE2USO6SmcLMpm6WGFWReXRCzOdDjm
19:05:00.763 [main] DEBUG org.apache.http.headers - >> Content-Length: 733
19:05:00.763 [main] DEBUG org.apache.http.headers - >> Host: clockbone.com
19:05:00.763 [main] DEBUG org.apache.http.headers - >> Connection: Keep-Alive
19:05:00.763 [main] DEBUG org.apache.http.headers - >> User-Agent: Apache-HttpClient/4.5.5 (Java/1.8.0_144)
19:05:00.763 [main] DEBUG org.apache.http.wire -  >> "--"
19:05:00.763 [main] DEBUG org.apache.http.wire -  >> "ujzwtsWv5RE2USO6SmcLMpm6WGFWReXRCzOdDjm"
19:05:00.763 [main] DEBUG org.apache.http.wire -  >> "[\r][\n]"
19:05:00.763 [main] DEBUG org.apache.http.wire -  >> "Content-Disposition"
19:05:00.763 [main] DEBUG org.apache.http.wire -  >> ": "
19:05:00.763 [main] DEBUG org.apache.http.wire -  >> "form-data; name="signture""
19:05:00.763 [main] DEBUG org.apache.http.wire -  >> "[\r][\n]"
19:05:00.763 [main] DEBUG org.apache.http.wire -  >> "[\r][\n]"
19:05:00.763 [main] DEBUG org.apache.http.wire -  >> "sdfssdfdsfdofE5kEazR++92EZBB1a5444s="
19:05:00.763 [main] DEBUG org.apache.http.wire -  >> "[\r][\n]"
19:05:00.763 [main] DEBUG org.apache.http.wire -  >> "--"
19:05:00.763 [main] DEBUG org.apache.http.wire -  >> "ujzwtsWv5RE2USO6SmcLMpm6WGFWReXRCzOdDjm"
19:05:00.763 [main] DEBUG org.apache.http.wire -  >> "[\r][\n]"
19:05:00.763 [main] DEBUG org.apache.http.wire -  >> "Content-Disposition"
19:05:00.763 [main] DEBUG org.apache.http.wire -  >> ": "
19:05:00.763 [main] DEBUG org.apache.http.wire -  >> "form-data; name="FileName""
19:05:00.763 [main] DEBUG org.apache.http.wire -  >> "[\r][\n]"
19:05:00.763 [main] DEBUG org.apache.http.wire -  >> "[\r][\n]"
19:05:00.763 [main] DEBUG org.apache.http.wire -  >> "1.png"
19:05:00.763 [main] DEBUG org.apache.http.wire -  >> "[\r][\n]"
19:05:00.763 [main] DEBUG org.apache.http.wire -  >> "--"
19:05:00.763 [main] DEBUG org.apache.http.wire -  >> "ujzwtsWv5RE2USO6SmcLMpm6WGFWReXRCzOdDjm"
19:05:00.763 [main] DEBUG org.apache.http.wire -  >> "[\r][\n]"
19:05:00.763 [main] DEBUG org.apache.http.wire -  >> "Content-Disposition"
19:05:00.763 [main] DEBUG org.apache.http.wire -  >> ": "
19:05:00.763 [main] DEBUG org.apache.http.wire -  >> "form-data; name="file"; filename="1.png""
19:05:00.763 [main] DEBUG org.apache.http.wire -  >> "[\r][\n]"
19:05:00.763 [main] DEBUG org.apache.http.wire -  >> "Content-Type"
19:05:00.763 [main] DEBUG org.apache.http.wire -  >> ": "
19:05:00.763 [main] DEBUG org.apache.http.wire -  >> "image/jpeg"
19:05:00.763 [main] DEBUG org.apache.http.wire -  >> "[\r][\n]"
19:05:00.763 [main] DEBUG org.apache.http.wire -  >> "[\r][\n]"
19:05:00.763 [main] DEBUG org.apache.http.wire -  >> "[0x89]PNG[\r][\n]"
19:05:00.763 [main] DEBUG org.apache.http.wire -  >> "[0x1a][\n]"
19:05:00.763 [main] DEBUG org.apache.http.wire -  >> "[0x0][0x0][0x0][\r]IHDR[0x0][0x0][0x0][0x1a][0x0][0x0][0x0][\r][0x8][0x2][0x0][0x0][0x0]8[0xfb][0xe9][0xba][0x0][0x0][0x0][0xc5]IDAT8[0x11]c[0xfc][0xf9][0xf3]'[0x3][0x6][0xf8][0xff][0xea][0x15]ci[0xe9]''[0x86][0x80][0x0]F~~[0xc]y[0x9c][0x2],Xd[0xb6]la[0x9a]?[0xff]W[0x17][0xa3][0xb2]2[0x16]Y[0xbc]B[0x18][0xc6][0xd5][0xd7]3pr[0xfe]_[0xbb][0x16][0xaf].[0x9c][0x92][0xa8][0xc6][0xd5][0xd6][0xfe][0xb7][0xb2]b[0xf4][0xf4][0xc4][0xa9][0x9c][0x90][0x4][0xc2]8[0xc6][0x88][0x88][0xbf][0xed][0xed]L[0x8a][0x8a][0x84][0xb4][0xe0][0x93]g[0x82]J[0xc6][0xc7][0xff]om[0xa5][0xd0],[0xa0]Q [0xe3][0x18][0xd3][0xd3][0xff][0x97][0x97]3[0x90][0x1e][0xf0][0x98][0xee]d[0xae][0xe5][0xe0][0xf8][0x1f][0x1c][0xcc][0xa8][0xaf][0x8f])G[0x86][0x8][0x13][0xc3][0x97]/[0x8c]&&d[0xe8][0xc4][0xaa][0x85][0xf1][0xc7][0xc7][0x8f][0x8c][0x1c][0x1c]X[0xe5][0xc8][0x10]d[0xa2][0xa2]Y@[0xeb]a1K[0x86]K[0xb0]i![0xca][0xb8][0x97]+[0xc3][0xc3]W[0xbe][0xc4][0xa6][0x9d][0x81][0xe1][0xe9][0x8a][0xf0][0xa8][0x15]p9[0x0][0x8b]`-[0x4][0x87][0x1b]K[0xa0][0x0][0x0][0x0][0x0]IEND[0xae]B`[0x82]"
19:05:00.778 [main] DEBUG org.apache.http.wire -  >> "[\r][\n]"
19:05:00.778 [main] DEBUG org.apache.http.wire -  >> "--"
19:05:00.778 [main] DEBUG org.apache.http.wire -  >> "ujzwtsWv5RE2USO6SmcLMpm6WGFWReXRCzOdDjm"
19:05:00.778 [main] DEBUG org.apache.http.wire -  >> "--"
19:05:00.778 [main] DEBUG org.apache.http.wire -  >> "[\r][\n]"
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值