文件流在Android到WCF [英] file streaming from Android to WCF
问题描述
我有以下合约:
[OperationContract(Name = "Upload")]
[WebInvoke(Method = "POST", UriTemplate = "/Upload/{step}/{fileName}",
BodyStyle= WebMessageBodyStyle.WrappedRequest)]
Response FileUpload(string fileName, string step, Stream fileStream);
和实施
public Response FileUpload(string fileName, string step, Stream fileStream)
{
FileStream fileToUpload = new FileStream("C:\\inetpub\\wwwroot\\Upload\\" + fileName, FileMode.Create);
byte[] bytearray = new byte[10000];
int bytesRead, totalBytesRead = 0;
do
{
bytesRead = fileStream.Read(bytearray, 0, bytearray.Length);
totalBytesRead += bytesRead;
} while (bytesRead > 0);
fileToUpload.Write(bytearray, 0, bytearray.Length);
fileToUpload.Close();
fileToUpload.Dispose();
Response res = new Response();
res.Successful = true;
res.Comment = "Bla bla";
return res;
}
和配置:
<system.serviceModel>
<client>
<endpoint address="" binding="webHttpBinding"
bindingConfiguration="StreamHTTP"
contract="BillboardServices.IBillboardService"
/>
</client>
<bindings>
<webHttpBinding>
<binding name="StreamedHTTP"
transferMode="StreamedRequest"
maxReceivedMessageSize="10000000"
maxBufferSize="1000"
/>
</webHttpBinding>
</bindings>
</system.serviceModel>
当我打电话通过Android的HTTP请求的方法,我得到的状态code 400 - 错误的请求。
我有相同的Web服务,这是工作的其他方法。
我只是想不通问题出在哪里。
When I call the method through Android http request, I get status code 400 - Bad Request. I have other methods on the same web service, which are working. I just can't figure out where the problem is.
推荐答案
我设置一个测试服务器的基础上,code你已经张贴以上。
I setup a test server, based on the code you have posted above.
我执行使用招以下...
I executed the following using Fiddler...
的http://本地主机:3333 /上传/步/文件名 HTTP / 1.1
POST http://localhost:3333/upload/step/filename HTTP/1.1
用户代理:提琴手
主持人:本地主机:3333
Host: localhost:3333
内容长度:11
<Foo></Foo>
和服务器上我调整你的code有点像这样...
And on the server I tweaked your code a bit to something like this...
public string FileUpload(string fileName, string step, Stream fileStream)
{
StreamReader reader = new StreamReader(fileStream);
Console.WriteLine(reader.ReadToEnd());
return "Done";
}
我没有issues.The code运行良好。
I had no issues.The code ran fine.
你确定你已经使用URI模板是没有得到冲突以或许一些其他的终点? (通常WebGet)
Are you sure the URI template you have used is not getting conflicted with some other endpoint perhaps? (usually WebGet)
这篇关于文件流在Android到WCF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!