Rest WCF服务器“远程服务器返回错误:(400)Bad Request。”内容大于64k

时间:2012-11-23 13:02:35

标签: asp.net wcf c#-4.0 rest

问题与WCF配置地狱有关,我开发了一个wcf休息服务器,我将需要它与iPhone和Android客户端一起使用。问题是我认为我的自定义配置没有提升,因为大于64kb的数据会在客户端上提供The remote server returned an error: (400) Bad Request.。这是我的代码

  • 服务器配置:

                                             

        <bindings>
          <webHttpBinding>
            <binding name="customHttpsBinding" openTimeout="00:10:00"
              receiveTimeout="00:10:00" sendTimeout="00:30:00" maxBufferSize="2147483647" maxBufferPoolSize="2147483647"
              maxReceivedMessageSize="2147483647" transferMode="Streamed"  crossDomainScriptAccessEnabled="true" >
              <readerQuotas maxDepth="999999999" maxStringContentLength="2147483647"
                maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2097151" />
              <security mode="Transport">
                <transport proxyCredentialType="Basic" />
              </security>
            </binding>
          </webHttpBinding>
        </bindings>
        <behaviors>
          <endpointBehaviors>
            <behavior name="webHttpBehaviour">
              <webHttp helpEnabled="true" defaultOutgoingResponseFormat="Json"
                automaticFormatSelectionEnabled="false"  faultExceptionEnabled="true"/>
              <dataContractSerializer maxItemsInObjectGraph="2147483647"/>
            </behavior>
          </endpointBehaviors>
          <serviceBehaviors>
            <behavior name="customServiceBehavior" >
              <serviceMetadata httpsGetEnabled="true" />
              <serviceDebug includeExceptionDetailInFaults="true" />
              <dataContractSerializer maxItemsInObjectGraph="2147483647"/>
            </behavior>
    
          </serviceBehaviors>
        </behaviors>
        <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" >
        </serviceHostingEnvironment>
      </system.serviceModel>
    

                 

服务器方法:

[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = "/content?contributorId={contributorId}&marketId={marketId}&fileExtension={fileExtension}",
    RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
ActionResult PostUerContent(string contributorId,int marketId,string fileExtension, Stream streamContent);

的Global.asax:

protected void Application_Start(object sender, EventArgs e)
{
    ServiceRoute serviceRoute = new ServiceRoute("api", new WebServiceHostFactory(), typeof(MobileAPI));
    RouteTable.Routes.Add(serviceRoute);
}

客户端代码(.Net)

Stream fileStream = new FileStream(Server.MapPath("~/desert_.jpg"), FileMode.Open);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://shaq.***.local/mobile/api/content?contributorId=10634&marketid=2&fileExtension=.jpg");
Stream serverStream = null;
try
{
    request.ContentType = "application/plain";
    request.Method = "POST";
    request.KeepAlive = true;
    request.ContentLength = fileStream.Length;
    request.SendChunked = true;
    request.AllowWriteStreamBuffering = false;
    serverStream = request.GetRequestStream();

    byte[] buffer = new byte[16384];
    while (true)
    {
        int bytesRead = fileStream.Read(buffer, 0, buffer.Length);
        if (bytesRead > 0)
        {
            serverStream.Write(buffer, 0, bytesRead);
        }
        else
        {
            break;
        }
     }

     request.GetResponse();

 }
 catch (Exception ex)
 { }
 finally
 {
     serverStream.Close();
     fileStream.Close();
 }

0 个答案:

没有答案