0

尝试上传图片时出现以下错误。

无法处理消息,因为内容类型为“multipart/form-data;” 边界=AwQm1pbogJ6qQuZlUZjJ6kNOvbehrlyozA-w' 不是预期的类型 'text/xml; 字符集=utf-8'。

安卓代码:

HttpPost httppost = new HttpPost("http://192.168.1.111/androidservice/MediaUploadService.svc/uploadFile");
File photo = new File(Environment.getExternalStorageDirectory(), "01.jpg");
MultipartEntity t = new MultipartEntity();
t.addPart("t", new FileBody(photo));
httppost.setEntity(t);
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response = httpclient.execute(httppost);

WCF 代码:

namespace AndroidService
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "MediaUploadService" in code, svc and config file together.
public class MediaUploadService : IMediaUploadService
{
    public void UploadFile(Stream fileContents)
    {
       byte[] buffer = new byte[10000];
        int bytesRead, totalBytesRead = 0;
        do
        {
            bytesRead = fileContents.Read(buffer, 0, buffer.Length);
            totalBytesRead += bytesRead;
        } while (bytesRead > 0);
        File.WriteAllText(@"D:\\Vechile\log2.txt", totalBytesRead.ToString());
    }
}
}

捆绑:

  <system.serviceModel>
<behaviors>
  <endpointBehaviors>
    <behavior name="httpBehavior">
      <webHttp />
    </behavior>
  </endpointBehaviors>

  <serviceBehaviors>
    <behavior name="">
      <serviceMetadata httpGetEnabled="true" />
      <serviceDebug includeExceptionDetailInFaults="false" />
    </behavior>

    <behavior name="PublishMetadataBehavior">
      <serviceMetadata httpGetEnabled="true" policyVersion="Policy15"/>
    </behavior>

  </serviceBehaviors>
</behaviors>

<bindings>
  <webHttpBinding>
    <binding name="WebHttpDtreaming" transferMode="Streamed" >
    </binding>
  </webHttpBinding>
</bindings>

<standardEndpoints>
  <webHttpEndpoint>
   <standardEndpoint name=""
                  helpEnabled="true"
                  automaticFormatSelectionEnabled="true"
                  maxReceivedMessageSize="1000000"/>
  </webHttpEndpoint>
</standardEndpoints>

<serviceHostingEnvironment multipleSiteBindingsEnabled="true" aspNetCompatibilityEnabled="true" />

我将不胜感激任何帮助。

4

1 回答 1

0

试试这个 httppost.setHeader("content-type", "application/json")

于 2012-12-01T14:02:29.487 回答