2011-10-15 59 views
0

我有以下合约:文件流媒体从安卓到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请求,我得到状态代码400 - 错误的请求。 我有相同的Web服务的其他方法,这是工作。 我只是想不通问题出在哪里。

+0

使用Wireshark来检查*实际*线路流量。 – Amy

+0

感谢您的想法。我在Android端和服务器端都尝试过它,它看起来像一个普通的多部分帖子。我不是一个大型的网络专家,但对我来说看起来很好。 –

回答

0

我设置一个测试服务器,根据您上面贴的代码。

我执行以下使用招...

POST http://localhost:3333/upload/step/filename HTTP/1.1

的User-Agent:提琴手

主机:本地主机:3333

的Content-Length:11

<Foo></Foo> 

和服务器上我调整ÿ我们的代码有点像这样...

public string FileUpload(string fileName, string step, Stream fileStream) 
    { 
     StreamReader reader = new StreamReader(fileStream); 
     Console.WriteLine(reader.ReadToEnd()); 
     return "Done"; 
    } 

我没有问题。代码运行良好。

你确定你已经使用中,无法与可能还有一些其他端点发生冲突的URI模板? (通常是WebGet)

+0

那么我创建了一个新项目。这次我使用了WCF ajax服务而不是WCF服务应用程序,现在一切正常。你是唯一回复的人,所以...玩得开心:) –