在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問(wèn)答/Java  網(wǎng)絡(luò)安全/ httpclient模擬postman發(fā)送請(qǐng)求?

httpclient模擬postman發(fā)送請(qǐng)求?

問(wèn)題描述

post方法調(diào)用遠(yuǎn)端接口,postman成功,自己通過(guò)java代碼使用httpclient請(qǐng)求失敗.
服務(wù)端不能夠調(diào)試,不知道自己的錯(cuò)誤 A server error occurred. Please contact the administrator

問(wèn)題出現(xiàn)的環(huán)境背景及自己嘗試過(guò)哪些方法

  1. 首先需要將multipartfile轉(zhuǎn)換為file
  2. 構(gòu)造的httpclient的entity中,使用的是MultipartEntity

相關(guān)代碼

// 請(qǐng)把代碼文本粘貼到下方(請(qǐng)勿用圖片代替代碼)

使用postman請(qǐng)求

postman構(gòu)造請(qǐng)求成功
clipboard.png

使用postman請(qǐng)求本地項(xiàng)目

header中設(shè)置了content-type 為form-data , 返回值為"A server error occurred. Please contact the administrator."
clipboard.png

controller層

@RequestMapping(value = "/image", method = {RequestMethod.POST})

@ResponseBody
public String getSimilarImage( int num, MultipartFile image) {

    HashMap<String, String> params = new HashMap<>();
    params.put(ConstantUtil.NUM, String.valueOf(num));

    String result = null;
    File file = null;
    try {

        // 類(lèi)型轉(zhuǎn)換使用
        file = new File(image.getOriginalFilename());
        FileUtils.copyInputStreamToFile(image.getInputStream(),file);

        result = HttpClientUtil.doFilePost(gunsProperties.getSimilarImage(), file, params);
        System.out.println(result);
    } catch (Exception e) {
        logger.debug(e.getMessage());
    }

    File del = new File(file.toURI());
    del.delete();
    return result;

}

httpclient的工具類(lèi)post方法

public static String doFilePost(String url, File file, HashMap<String, String> params) {

    CloseableHttpClient httpClient = null;
    CloseableHttpResponse response = null;
    String resultString = "";
    try {
        httpClient = HttpClients.createDefault();
        RequestConfig requestConfig = RequestConfig.custom().
                setConnectTimeout(2000000000).
                setConnectionRequestTimeout(2000000000).
                setSocketTimeout(2000000000).build();
        // 創(chuàng)建Http Post請(qǐng)求
        HttpPost httpPost = new HttpPost(url);
        httpPost.setHeader(ConstantUtil.CONTENT_TYPE, ConstantUtil.CONTENT_TYPE_VALUE);
        httpPost.setConfig(requestConfig);
        /*httpPost.setProtocolVersion(HttpVersion.HTTP_1_0);
        httpPost.addHeader(HTTP.CONN_DIRECTIVE, HTTP.CONN_CLOSE);*/

        // 設(shè)置Header信息
        MultipartEntityBuilder builder = MultipartEntityBuilder.create();
        builder.addPart("image", new FileBody(file));

        if (params != null && !params.isEmpty()) {
            for (Map.Entry<String, String> entry : params.entrySet()) {
                StringBody contentBody = new StringBody(entry.getValue(),
                        "text/plain", Charset.forName("UTF-8"));
                builder.addPart(entry.getKey(), contentBody);
            }
        }

        HttpEntity entity = builder.build();

        httpPost.setEntity(entity);
        response = httpClient.execute(httpPost);
        HttpEntity responseEntity = response.getEntity();
        if (responseEntity != null) {
            resultString = EntityUtils.toString(responseEntity, Charset.forName("UTF-8"));
        }
    } catch (Exception e) {
        logger.debug("超時(shí)異常,異常為{},異常信息為{}", e, e.getMessage());
        MsgResponse.failMessage(ConstantUtil.OUT_OF_DATE);
    } finally {
        try {
            if (response != null) {
                response.close();

            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    try {
        httpClient.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return resultString;
}

你期待的結(jié)果是什么?實(shí)際看到的錯(cuò)誤信息又是什么?

期待和postman一樣的結(jié)果,不知道中間哪個(gè)環(huán)節(jié)設(shè)置錯(cuò)誤

回答
編輯回答
念舊

原因是post中不能設(shè)置header,使用multipartfile的時(shí)候,已經(jīng)默認(rèn)進(jìn)行了使用

2017年2月4日 04:38