HttpPost 中的边界

Boundary In HttpPost

我正在处理一些要发送到服务器的 HttpPost 请求。但是我坚持使用我不知道的 "boundary" 。这就是文档中的示例所说的内容:

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

就是header。但是我对其中提到的边界事物一无所知。你能举一个工作的例子来解释这个吗?它究竟是什么?你要将这个发送到服务器吗?

这就是我目前所做的。

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();
        }

    }

}

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

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

没有边界,它假定整个响应是同类的(或用户解析的)。你提供了一个例子。根据您设置的 server/clients,它可以通过 POST 设置到服务器,或者通过 GET 从服务器设置为 return。