使用 WCF 模板 40 将文件从 Android (2.3.4) 上传到 WCF 4.0 REST 服务
我正在尝试使用下面的代码从 Android (2.3.4) 上传文件,我知道如果我也想发送参数,我必须使用多部分发布。这是我的 Android 代码:
public void uploadFile(File uploadFile) {
HttpClient httpClient = new DefaultHttpClient();
HttpPost postRequest = new HttpPost("http://192.168.0.102/application/services/sendfile/");
ResponseHandler<String> responseHandler = new BasicResponseHandler();
// Indicate that this information comes in parts (text and file)
MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
try {
//Create a JSON object to be used in the StringBody
JSONObject jsonObj = new JSONObject();
//Add some values
jsonObj.put("filename", uploadFile.getName());
//Add the JSON "part"
reqEntity.addPart("entity", new StringBody(jsonObj.toString()));
}
catch (JSONException e) {
Log.v("App", e.getMessage());
}
catch (UnsupportedEncodingException e) {
Log.v("App", e.getMessage());
}
FileBody fileBody = new FileBody(uploadFile);//, "application/octet-stream");
reqEntity.addPart("file", fileBody);
try {
postRequest.setEntity(reqEntity);
//Execute the request "POST"
HttpResponse httpResp = httpClient.execute(postRequest);
//Check the status code, in this case "created"
if(((HttpResponse) response).getStatusLine().getStatusCode() == HttpStatus.SC_CREATED){
Log.v("App","Created");
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
它总是检索错误的请求。
另一方面,我有一个使用模板 40 制作的 WCF REST 服务:
[WebInvoke(
Method = "POST",
UriTemplate = "sendFile",
BodyStyle = WebMessageBodyStyle.Wrapped)]
public int sendFile(Stream receivedFile)
{
// How should I process the multipart file here to save it with its name on disk?
}
我在 web.config 中有这个
<system.webServer>
<modules runAllManagedModulesForAllRequests="true">
<remove name="WebDAVModule" />
<add name="UrlRoutingModule" type="System.Web.Routing.UrlRoutingModule, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
</modules>
<directoryBrowse enabled="false" />
</system.webServer>
<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
<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>
</system.serviceModel>
,在我的 global.asax 中有这个。
private void RegisterRoutes()
{
RouteTable.Routes.Add(new ServiceRoute("Service", new WebServiceHostFactory(), typeof(AccidentService)));
}
知道为什么如果我在其中放置断点,WCF 不会收到该文件吗?
如果有人有关于如何将文件从 Android (2.3.4) 发送到 WCF 4.0 的更好示例,我们将不胜感激。
提前致谢!吉列尔莫.
I'm trying to upload a file from Android (2.3.4) using the below code, I understand I must use multipart post if I want to send parameter as well. This is my Android code:
public void uploadFile(File uploadFile) {
HttpClient httpClient = new DefaultHttpClient();
HttpPost postRequest = new HttpPost("http://192.168.0.102/application/services/sendfile/");
ResponseHandler<String> responseHandler = new BasicResponseHandler();
// Indicate that this information comes in parts (text and file)
MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
try {
//Create a JSON object to be used in the StringBody
JSONObject jsonObj = new JSONObject();
//Add some values
jsonObj.put("filename", uploadFile.getName());
//Add the JSON "part"
reqEntity.addPart("entity", new StringBody(jsonObj.toString()));
}
catch (JSONException e) {
Log.v("App", e.getMessage());
}
catch (UnsupportedEncodingException e) {
Log.v("App", e.getMessage());
}
FileBody fileBody = new FileBody(uploadFile);//, "application/octet-stream");
reqEntity.addPart("file", fileBody);
try {
postRequest.setEntity(reqEntity);
//Execute the request "POST"
HttpResponse httpResp = httpClient.execute(postRequest);
//Check the status code, in this case "created"
if(((HttpResponse) response).getStatusLine().getStatusCode() == HttpStatus.SC_CREATED){
Log.v("App","Created");
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
It's always retrieving bad request.
On the other side I have a WCF REST service made using Template 40:
[WebInvoke(
Method = "POST",
UriTemplate = "sendFile",
BodyStyle = WebMessageBodyStyle.Wrapped)]
public int sendFile(Stream receivedFile)
{
// How should I process the multipart file here to save it with its name on disk?
}
I have this in my web.config
<system.webServer>
<modules runAllManagedModulesForAllRequests="true">
<remove name="WebDAVModule" />
<add name="UrlRoutingModule" type="System.Web.Routing.UrlRoutingModule, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
</modules>
<directoryBrowse enabled="false" />
</system.webServer>
<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
<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>
</system.serviceModel>
and this in my global.asax
private void RegisterRoutes()
{
RouteTable.Routes.Add(new ServiceRoute("Service", new WebServiceHostFactory(), typeof(AccidentService)));
}
Any idea why the WCF doesn't receive the file if I put a breakpoint in it?
If anyone has a better example on how to send a file from Android (2.3.4) to WCF 4.0 will be really appreciated.
Thanks in advance! Guillermo.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题在于 WCF 中的绑定!改变它们就可以了!
The problem was with the bindings in the WCF! changing them made it work!