【发布时间】:2011-06-30 00:10:58
【问题描述】:
我正在构建一个 Android 应用,我需要上传一张用相机拍摄的照片。我需要将它上传到一个安静的 WCF 服务。我看过很多教程,但我似乎无法让它工作。我认为我的问题在于 WCF 服务。我没有收到任何异常,但我收到了 400 BAD REQUEST 响应。由于 WCF 服务当前正在我的本地主机上运行,因此我使用 10.0.2.2 从 android 模拟器访问它。我可以在本地服务上调用其他服务方法,但这个方法失败了。
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
}
}
配置文件
<?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>
我做错了什么?
【问题讨论】:
-
您能否从不同的客户端(例如:您在同一解决方案中创建的测试客户端)调用 WCF 方法?如果不是,那么问题可能出在您的服务代码中,甚至更有可能出在您的配置中。再说一次,可能只是您尝试使用的 URI 不正确...
-
是的。我使用 Fiddler 并在模拟器中使用 android 浏览器在同一服务中调用了另一种方法。两者都有效。只是这个特殊的调用在我的 Android 应用程序中不起作用。
-
我认为你至少应该删除 getExternalStorageDirectory() 后面的逗号。
-
@jcruz 你有想过这个吗?我现在也有同样的问题。谢谢。
标签: android wcf file-upload