問題は 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();
}