【问题标题】:Boundary In HttpPostHttpPost 中的边界
【发布时间】:2016-03-10 03:25:06
【问题描述】:

我正在处理一些要发送到服务器的 HttpPost 请求。但我被我不知道的“边界”所困。这就是文档中的示例所说的:

Request:
POST /render?method=addJob&version=1000&target=stream HTTP/1.1
Content-Type: multipart/form-data; boundary=boundary
--boundary
Content-Length: ...
Chapter 2 Description of the HTTP interface
18 OpenText Rendition Server RS100000-PGD-EN
X-sourceType: options
X-optionsAs: properties
pageSize=A4
serverSideProfile=Confidential
--boundary
Content-Length: ...
X-sourceType: stream
Content-Disposition: form-data; filename="sourceFile1.xls"
fadjföajdfhaor ofua rela üjadfhkhdaf adfaj f (binary content)
--boundary
Content-Length: ...
X-sourceType: stream
Content-Disposition: form-data; filename="sourceFile2.doc"
X-urlForm: http://server/path/file.jpg
X-formParams: overlayLayer=Background&overlayAlignment=TopLeft
fadjföajdfhaor ofua rela üjadfhkhdaf adfaj f (binary content)
--boundary
Content-Length: ...
X-sourceType: url
X-url:
http://server:8080/archive?get&contRep=WM_101&docId=ICS_1&pVersion=0045
X-contentType: image/tiff
--boundary--

我能够弄清楚那部分

Content-Type: multipart/form-data; boundary=boundary

是标题。但我不知道其中提到的边界事物。您能否举一个工作示例来解释这一点?它实际上是什么?你把它发送到服务器?

这就是我现在正在做的事情。

package method;

import java.io.IOException;

import javax.swing.text.html.parser.Entity;

import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.impl.client.DefaultHttpClient;

import paramStorage.Data;

public class AddJob {

    private HttpClient client = null;
    private String URL = null;
    private HttpPost post = null;
    private HttpResponse response = null;
    private final String boundary = "--boundary\r\n" + 
                                    "X-sourceType: stream\r\n"+
                                    "pageSize=A4"+
                                    "serverSideProfile=Confidential"+
                                    "--boundary--\r\n";

    public void getSessionId() {
        client = new DefaultHttpClient();

        URL = "http://" + Data.server + ":" + Data.port + "/render?method=addJob&version=1000&target=" + Data.target
                + "&targetContentType=" + Data.targetContentType;
        System.out.println(URL);
        post = new HttpPost(URL);
        post.addHeader("Content-Type","multipart/form-data; boundary=boundary");
        String body = "--boundary\r\n"+
                "X-sourceType: stream\r\n"+
                "emailFrom: test@domain.com\r\n"+
                "emailTo: test2@domain.com\r\n"+
                "emailSubject: Java Mail\r\n"+
                "--boundary--\r\n";

        try {
            HttpEntity entity = new ByteArrayEntity(body.getBytes("UTF-8"));
            post.setEntity(entity);
            Header[] headerst = post.getAllHeaders();
            for (Header header : headerst) {
                System.out.println(header);
            }

            response = client.execute(post);
            if (response != null) {

                System.out.println("Status :: " + response.getStatusLine());
                Header[] headers = response.getAllHeaders();
                for (Header header : headers) {
                    System.out.println(header);
                }
                Data.jsessionid = headers[2].getValue();
                System.out.println(Data.jsessionid);

            }
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}

希望这能让您了解我想做什么。

【问题讨论】:

    标签: java http-post boundary


    【解决方案1】:

    边界是划分消息的各个部分。除了图像或其他二进制数据之外,我还使用它来返回一些 XML。我正在做视觉分析,除了车辆图像外,还会返回跟踪记录(以 xml 或 json 格式)。我也用它来传输相机校准。校准结构的各个部分以 xml/json/etc 的形式在一个区域中发送,而在另一个区域中是一个二进制表,将每个像素映射到其真实世界的位置(纬度、经度、高度。

    没有边界,它假设整个响应是同质的(或用户解析的)。你提供了一个例子。根据您设置的服务器/客户端,可以使用 POST 将其设置为服务器,也可以使用 GET 从服务器返回。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-05-26
      • 1970-01-01
      • 1970-01-01
      • 2021-06-21
      相关资源
      最近更新 更多