HttpPost中的边界

时间:2015-08-19 05:54:41

标签: java http-post boundary

我正在处理一些要发送到服务器的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();
        }

    }

}

希望这会让你知道我想做什么。

1 个答案:

答案 0 :(得分:0)

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

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