采用Android发布到XML文件 [英] Using Android to Post XML files

查看:142
本文介绍了采用Android发布到XML文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编辑:

我试图发送一个XML文件作为Android的一个职位的要求。

I am trying to send an xml file as a post request in Android.

服务器接受为text / xml。我试图创造一个MultipartEntity,它具有的multipart / form-data的内容类型。

The server accepts text/xml. I tried creating a MultipartEntity, which has a content type of multipart/form-data.

 HttpClient httpClient = new DefaultHttpClient();

    /* New Post Request */
    HttpPost postRequest = new HttpPost(url);

    byte[] data = IOUtils.toByteArray(payload);

    /* Body of the Request */
     InputStreamBody isb = new InputStreamBody(new ByteArrayInputStream(data), "uploadedFile");
    MultipartEntity multipartContent = new MultipartEntity();
    multipartContent.addPart("uploadedFile", isb);

    /* Set the Body of the Request */
    postRequest.setEntity(multipartContent);

    /* Set Authorization Header */
    postRequest.setHeader("Authorization", authHeader);
    HttpResponse response = httpClient.execute(postRequest);
    InputStream content = response.getEntity().getContent();
    return content;

不过,我得到一个错误说的内容类型,不能食用。

However I get an error saying the content type cannot be consumed.

The server refused this request because the request entity is in a format not supported by the requested resource for the requested method (Cannot consume content type).

如何更改请求的内容类型?

编辑:

推荐答案

长话短说 - 使用另一个构造为您InputStreamBody,让您指定要使用的MIME类型。如果不这样做,你的多部分请求的部分将不会有一个内容类型规定(详见下文)。因此,服务器不知道是什么类型的文件,并在你的情况可能会拒绝接受它(我接纳了他们,无论如何,但我认为这是由配置驱动)。如果这样仍然无法正常工作,你可能有一个服务器端的问题。

Long story short - use another constructor for your InputStreamBody that lets you specify the mime type you wish to use. If you don't, the parts in your multipart request will not have a Content-Type specified (see below for details). Consequently, the server does not know what type the file is, and in your case might be refusing to accept it (mine accepted them anyway, but I assume this is driven by config). If this still doesn't work, you might have a server-side issue.

注:更改内容类型要求自己什么,但的multipart / form-data的的;边界= someBoundary 渲染请求无效;服务器将不能正确解析多部分的部分。

Note: Changing the Content-Type of the request itself to anything but multipart/form-data; boundary=someBoundary renders the request invalid; the server will not correctly parse the multipart parts.

长很长的故事 - 这是我的结论

Long story long - here's my findings.

由于以下code:

byte[] data = "<someXml />".getBytes();
multipartContent.addPart("uploadedFile", new InputStreamBody(new ByteArrayInputStream(data), "text/xml", "somefile.xml"));
multipartContent.addPart("otherPart", new StringBody("bar", "text/plain", Charset.forName("UTF-8")));
multipartContent.addPart("foo", new FileBody(new File("c:\\foo.txt"), "text/plain"));

HttpClient的帖子以下的有效载荷(捕获W / Wireshark的):

The HttpClient posts the following payload (captured w/ Wireshark):

POST /upload.php HTTP/1.1
Transfer-Encoding: chunked
Content-Type: multipart/form-data; boundary=SeXc6P2_uEGZz9jJG95v2FnK5a8ozU8KfbFYw3
Host: thehost.com
Connection: Keep-Alive
User-Agent: Apache-HttpClient/4.1-alpha2 (java 1.5)

--SeXc6P2_uEGZz9jJG95v2FnK5a8ozU8KfbFYw3
Content-Disposition: form-data; name="uploadedFile"; filename="someXml.xml"
Content-Type: text/xml
Content-Transfer-Encoding: binary

<someXml />
--SeXc6P2_uEGZz9jJG95v2FnK5a8ozU8KfbFYw3
Content-Disposition: form-data; name="otherPart"
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

yo
--SeXc6P2_uEGZz9jJG95v2FnK5a8ozU8KfbFYw3
Content-Disposition: form-data; name="foo"; filename="foo.txt"
Content-Type: text/plain
Content-Transfer-Encoding: binary

Contents of foo.txt

--SeXc6P2_uEGZz9jJG95v2FnK5a8ozU8KfbFYw3--

在服务器上,下面的PHP脚本:

On the server, the following PHP script:

<?php
print_r($_FILES);
print_r($_REQUEST);

吐尽了以下工作:

spitted out the following:

Array
(
    [uploadedFile] => Array
        (
            [name] => someXml.xml
            [type] => text/xml
            [tmp_name] => /tmp/php_uploads/phphONLo3
            [error] => 0
            [size] => 11
        )

    [foo] => Array
        (
            [name] => foo.txt
            [type] => text/plain
            [tmp_name] => /tmp/php_uploads/php58DEpA
            [error] => 0
            [size] => 21
        )

)
Array
(
    [otherPart] => yo
)

这篇关于采用Android发布到XML文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆