How do I upload a file from an android device to a restful wcf service?
I'm building an Android app and I need to upload a photo taken with the camera. I need to upload it to a restful WCF service. I have looked at many tutorials but I just can't seem to get it working. I think my problem lies with the WCF service. I do not get any exceptions but I get a response 400 BAD REQUEST. Since the WCF service is currently running on my localhost, I am using 10.0.2.2 to access it from the android emulator. I am able to call other service methods on my local service but this one fails.
Java
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
}
}
Config file
<?xml version="1.0"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
<customErrors mode="Off"></customErrors>
</system.web>
<system.webServer>
<modul开发者_如何学编程es 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>
What am I doing wrong?
Is using the WebHttpBinding raw programming model on the service side an option for you? See these two blog posts for detailed step-by-step instructions on how to pull it off.
For anyone that comes across this, as I just did, it turned out that I needed to update the maxReceivedMessageSize for my WCF service. This question helped: WCF REST service 400 Bad Request
精彩评论