简体   繁体   English

将POSTMAN代码转换成Java

[英]Translate POSTMAN code into Java

I am trying to implement (in java) POSTMAN preview of POST request. 我正在尝试实现(在Java中)POST请求的POSTMAN预览。 it uses boundary and content-disposition, i am not so much familiar with boundary and content disposition implementation in java. 它使用边界和内容处置,我不太熟悉Java中的边界和内容处置实现。 I think experts can help me out from this problem, thanks in advance. 我想专家们可以帮助我解决这个问题,谢谢。

POST /etap-cgi/cgiunl.exe HTTP/1.1
Host: unl.ru
X-Auth-Token: 5dc347210229bd8b9565d213f0d4f80f738970083d03a8e73a0d5f649019272e
Cache-Control: no-cache

----WebKitFormBoundaryE19zNvXGzXaLvS5C
Content-Disposition: form-data; name="conversion"

true
----WebKitFormBoundaryE19zNvXGzXaLvS5C
Content-Disposition: form-data; name="language"

en
----WebKitFormBoundaryE19zNvXGzXaLvS5C
Content-Disposition: form-data; name="data"

aar partasi nah
----WebKitFormBoundaryE19zNvXGzXaLvS5C
Content-Disposition: form-data; name="outputmode"

text
----WebKitFormBoundaryE19zNvXGzXaLvS5C
Content-Disposition: form-data; name="coding"

utf-8
----WebKitFormBoundaryE19zNvXGzXaLvS5C

I have tried but it says' 我已经尝试过,但是它说'

Fatal error in process of translation 翻译过程中出现致命错误

Here is my implementation in java. 这是我在Java中的实现。

HttpClient httpclient = new DefaultHttpClient();

    try {
        // url parameters
        MultipartEntityBuilder multipartEntity = MultipartEntityBuilder
                .create();
        multipartEntity.addTextBody("conversion", "ture",
                ContentType.TEXT_PLAIN);
        multipartEntity.addTextBody("language", "en",
                ContentType.TEXT_PLAIN);
        multipartEntity.addTextBody("data", "example",
                ContentType.TEXT_PLAIN);
        multipartEntity.addTextBody("outputmode", "text",
                ContentType.TEXT_PLAIN);
        multipartEntity.addTextBody("coding", "utf-8",
                ContentType.TEXT_PLAIN);

        multipartEntity
                .setBoundary("----WebKitFormBoundaryE19zNvXGzXaLvS5C");


        HttpEntity multiPart = multipartEntity.build();

        HttpPost httpPost = new HttpPost(
                url);
        httpPost.setEntity(multiPart);
        httpPost.setHeader("Content-Disposition", "form-data");

        // get response after execution
        HttpResponse response = httpclient.execute(httpPost);
        // get response entities
        HttpEntity resEntity = response.getEntity();

        if (resEntity != null) {
            System.out.println("Response content length: "
                    + resEntity.getContentLength());
            String responseBody = EntityUtils.toString(resEntity);
            System.out.println("Data: " + responseBody);
        }

        EntityUtils.consume(resEntity);

    } catch (Exception err) {
        System.err.println(err.toString());
    } finally {
        // close connection
        httpclient.getConnectionManager().shutdown();
    }

I have solved my problem by myself with uploading a file instead of multi-part string, here is the soln with the boundary param without "WebKitFormBoundary" characters. 我已经通过上载文件而不是多部分字符串来解决了自己的问题,这是带有边界参数的解决方案,其中没有“ WebKitFormBoundary”字符。 hope this will helpful for someone. 希望这对某人有帮助。

public String httpOperation(String conversion, String fileName) {

    HttpClient httpclient = new DefaultHttpClient();
    try {
        // url parameters
        MultipartEntityBuilder multipartEntity = MultipartEntityBuilder
                .create();
        // add html tags param
        multipartEntity.addTextBody("conversion", conversion,
                ContentType.TEXT_PLAIN);
        multipartEntity.addTextBody("language", EN, ContentType.TEXT_PLAIN);
        multipartEntity.addTextBody("outputmode", TEXT,
                ContentType.TEXT_PLAIN);
        multipartEntity.addTextBody("coding", UTF, ContentType.TEXT_PLAIN);

        // add files as attachments
        multipartEntity.addPart("sourcefile", new FileBody(new File(
                fileName), ContentType.TEXT_PLAIN, "filename"));

        multipartEntity.setBoundary(PARAM_BOUNDARY);

        HttpEntity postEntity = multipartEntity.build();

        HttpPost httpPost = new HttpPost(URL);
        httpPost.setHeader("Content-Disposition", "form-data;");
        httpPost.setEntity(postEntity);

        // get response after execution
        HttpResponse response = httpclient.execute(httpPost);

        // get response entities
        HttpEntity resEntity = response.getEntity();

        if (resEntity != null) {
            @SuppressWarnings("deprecation")
            String responseBody = EntityUtils.toString(resEntity,
                    HTTP.UTF_8);
            // print output
            if (conversion.equals(TRUE)) {
                extractResponse(responseBody.toString());
                // System.out.println(responseBody.toString());
                result = responseBody.toString();
            } else {
                // System.out.println(responseBody.toString());
                result = responseBody.toString();
            }
        }
        EntityUtils.consume(resEntity);

    } catch (Exception err) {
        System.err.println(err.toString());
    } finally {
        // close connection
        httpclient.getConnectionManager().shutdown();
    }

    return result;
}

Here is the POSTMAN build preview 这是POSTMAN构建预览

POST /url/ HTTP/1.1
Host: unl.ru
X-Auth-Token: 5dc347210229bd8b9565d213f0d4f80f738970083d03a8e73a0d5f649019272e
Cache-Control: no-cache

----WebKitFormBoundaryE19zNvXGzXaLvS5C
Content-Disposition: form-data; name="conversion"

false
----WebKitFormBoundaryE19zNvXGzXaLvS5C
Content-Disposition: form-data; name="language"

en
----WebKitFormBoundaryE19zNvXGzXaLvS5C
Content-Disposition: form-data; name="sourcefile";   filename="conversion.txt"
Content-Type: text/plain


----WebKitFormBoundaryE19zNvXGzXaLvS5C
Content-Disposition: form-data; name="outputmode"

text
----WebKitFormBoundaryE19zNvXGzXaLvS5C
Content-Disposition: form-data; name="coding"

utf-8
----WebKitFormBoundaryE19zNvXGzXaLvS5C

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM