MultipartEntity 未创建良好的请求

发布于 10-28 21:04 字数 1006 浏览 4 评论 0原文

我需要将一些 XML 发送到 Web 服务,并且我能够使用普通的 StringEntity 来完成此操作,因为它只是文本,但现在我还需要向其附加图像。我尝试使用 MultipartEntity 进行此操作,但仅使用 XML 无法使其工作。

// 工作

DefaultHttpClient httpclient = new DefaultHttpClient();
HttpPost httppost doc = new HttpPost("http://mywebservices.com");

HttpEntity entity = new StringEntity(writer.toString());
httppost.setEntity(entity);

HttpResponse response = httpclient.execute(httppost);
HttpEntity responseEntity = response.getEntity();

// 不工作

DefaultHttpClient httpclient = new DefaultHttpClient();
HttpPost httppost doc = new HttpPost("http://mywebservices.com");

// no difference when removing the BROWSER_COMPATIBLE   
MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE); 
entity.addPart("xml", new StringBody(writer.toString()));
httppost.setEntity(entity);

HttpResponse response = httpclient.execute(httppost);
HttpEntity responseEntity = response.getEntity();

有没有办法让我看到正在发送的 MIME?

I needed to send some XML to a webservices and I was able to do it with a normal StringEntity because it was just text but now I need to attach an image to it as well. I tried doing it with a MultipartEntity but I couldn't get it working with just the XML.

// Working

DefaultHttpClient httpclient = new DefaultHttpClient();
HttpPost httppost doc = new HttpPost("http://mywebservices.com");

HttpEntity entity = new StringEntity(writer.toString());
httppost.setEntity(entity);

HttpResponse response = httpclient.execute(httppost);
HttpEntity responseEntity = response.getEntity();

// not working

DefaultHttpClient httpclient = new DefaultHttpClient();
HttpPost httppost doc = new HttpPost("http://mywebservices.com");

// no difference when removing the BROWSER_COMPATIBLE   
MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE); 
entity.addPart("xml", new StringBody(writer.toString()));
httppost.setEntity(entity);

HttpResponse response = httpclient.execute(httppost);
HttpEntity responseEntity = response.getEntity();

And is there a way I could see the MIME that is being send?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

心安伴我暖2024-11-04 21:04:20

您只是忘记了:

httppost.setEntity(entity);

顺便说一句,设置部件的 Content-Type 可能是一种很好的形式,例如:

entity.addPart("xml", new StringBody(writer.toString(),"application/xml",Charset.forName("UTF-8")));

至于查看发送的内容,请参阅 http://hc.apache.org/httpcomponents-client-ga/logging.html(尤其是“线路日志记录”)用于 HttpClient 日志记录功能, 和
这个问题了解如何让它工作在安卓上。

You simply forgot:

httppost.setEntity(entity);

By the way, it's probably good form to set the Content-Type of your parts, e.g.:

entity.addPart("xml", new StringBody(writer.toString(),"application/xml",Charset.forName("UTF-8")));

As far as seeing what's being sent, see http://hc.apache.org/httpcomponents-client-ga/logging.html (especially "wire logging") for the HttpClient logging features, and
this question for how to get it working on Android.

匿名的好友2024-11-04 21:04:20

查看正在发送的内容的另一种方法是设置您自己的“服务器”来接收请求。您可以使用 netcat 在类 Unix 系统上执行此操作。命令

nc -l 1234

行启动服务器侦听端口 1234,并回显收到的任何内容。

如果该“服务器”位于计算机 10.1.2.3 上,则只需使用 new HttpPost("http://10.1.2.3:1234") 将消息发送到那里。

Another way to see what's being sent is to set up your own "server" to receive the request. You can do this on a Unix-like system with netcat. The command-line

nc -l 1234

starts a server listening on port 1234, and will echo whatever is received.

If that "server" is on a machine 10.1.2.3, you can just use a new HttpPost("http://10.1.2.3:1234") to send the message there.

ㄟ。诗瑗2024-11-04 21:04:20

我有类似的问题,但我将带有用户/通行证的多部分发送到 acegi 安全系统,它适用于此:

request.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));

但不适用于此:

try {   
    for (NameValuePair param : params) {
        multientity.addPart(param.getName(), new StringBody(param.getValue(),      Charset.forName(encoding))));
    }
    request.setEntity(multientity);
}

I have a similar problem but I'm sending the multipart with user/pass to a acegi security system, it works with this:

request.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));

But not with this:

try {   
    for (NameValuePair param : params) {
        multientity.addPart(param.getName(), new StringBody(param.getValue(),      Charset.forName(encoding))));
    }
    request.setEntity(multientity);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文