如何将文件从Android设备上传到安静的wcf服务?

时间:2011-02-18 17:51:21

标签: android wcf file-upload

我正在构建一个Android应用,我需要上传用相机拍摄的照片。我需要将它上传到一个宁静的WCF服务。我看了很多教程,但我似乎无法让它工作。我认为我的问题在于WCF服务。我没有任何例外,但我得到了400 BAD REQUEST的回复。由于WCF服务当前正在我的localhost上运行,我使用10.0.2.2从android模拟器访问它。我能够在我的本地服务上调用其他服务方法,但是这个方法失败了。

爪哇

HttpPost httppost = new HttpPost("http://10.0.2.2:53943/ImageService/UploadInspectionPhoto");
File photo = new File(Environment.getExternalStorageDirectory(), "01.jpg");
MultipartEntity t = new MultipartEntity();
t.addPart("t", new FileBody(photo));
//t.addPart(new FormBodyPart("t", new FileBody(photo))); I tired this too
httppost.setEntity(t);
HttpResponse response = httpclient.execute(httppost);

WCF

[ServiceContract]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
public class ImageService
{
        [WebInvoke(Method = "POST")]
        public void UploadInspectionPhoto(Stream t)
        {
            // I put a breakpoint here but it never gets here
            // Do something with the stream
        }
}

配置文件

<?xml version="1.0"?>
<configuration>

  <system.web>
    <compilation debug="true" targetFramework="4.0" />
      <customErrors mode="Off"></customErrors>
  </system.web>

  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true">
      <add name="UrlRoutingModule" type="System.Web.Routing.UrlRoutingModule, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
    </modules>
  </system.webServer>

  <system.serviceModel>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
      <bindings>
          <!--<basicHttpBinding>
              <binding name="BasicHttpStreaming" transferMode="Streamed">
              </binding>
          </basicHttpBinding>-->
          <webHttpBinding>
              <binding name="WebHttpDtreaming" transferMode="Streamed" >
              </binding>
          </webHttpBinding>
      </bindings>
    <standardEndpoints>
      <webHttpEndpoint>
        <!-- 
            Configure the WCF REST service base address via the global.asax.cs file and the default endpoint 
            via the attributes on the <standardEndpoint> element below
        -->
        <standardEndpoint name="" helpEnabled="true" automaticFormatSelectionEnabled="true" />
      </webHttpEndpoint>
    </standardEndpoints>
      <services>
          <service behaviorConfiguration="PublishMetadataBehavior" name="SystemDesign.Collaborate.Services.ImageService">
              <endpoint address="soap" binding="basicHttpBinding" name="soap" contract="SystemDesign.Collaborate.Services.ImageService"/>
              <endpoint address="mex" binding="mexHttpBinding" name="mex" contract="IMetadataExchange" />
          </service>
      </services>
      <behaviors>
          <serviceBehaviors>
              <behavior name="PublishMetadataBehavior">
                  <serviceMetadata httpGetEnabled="true" policyVersion="Policy15"/>
              </behavior>
          </serviceBehaviors>
      </behaviors>
  </system.serviceModel>

</configuration>

我做错了什么?

2 个答案:

答案 0 :(得分:0)

在服务端使用WebHttpBinding原始编程模型是一个选项吗?有关如何将其删除的详细分步说明,请参阅these two博文。

答案 1 :(得分:0)

对于遇到此问题的任何人,正如我刚才所做的那样,事实证明我需要为我的WCF服务更新maxReceivedMessageSize。这个问题有帮助: WCF REST service 400 Bad Request