服务器代码:
WebServiceHost w = new WebServiceHost(typeof(WebHost), new Uri("http://localhost/Host");
BasicHttpBinding binding = new BasicHttpBinding();
binding.MaxBufferPoolSize = 2147483647;
binding.MaxBufferSize = 2147483647;
binding.MaxReceivedMessageSize = 2147483647;
binding.ReaderQuotas = new XmlDictionaryReaderQuotas { MaxStringContentLength = 2147483647 };
w.AddServiceEndpoint(typeof(WebHost), binding, "http://localhost/Host");
w.Open();
[ServiceContract]
public class HEWebHost
{
[OperationContract]
[WebInvoke(UriTemplate = "Host")]
public string Host(string largeRequest)
{
// ... Some code
}
}客户端代码:
HttpWebRequest request = HttpWebRequest.Create("http://localhost/Host") as HttpWebRequest;
request.Method = "POST";
StreamWriter writer = new StreamWriter(request.GetRequestStream());
writer.Write(largeRequestString);
writer.Flush();
writer.Close();
writer.Dispose();
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
StreamReader reader = new StreamReader(response.GetResponseStream());
string output = reader.ReadToEnd();即使我设置了绑定对象的MaxReceivedMessageSize,我仍然得到“400Bad Request”。完全相同的代码,只是输入的字符串很少,所以...我如何让这段代码能处理更大的输入字符串呢?
发布于 2013-06-10 20:25:03
您的代码中存在错误。您正在使用WebServiceHost (WCF REST编程模型)和basicHttpbinding (WCF SOAP编程模型)。您不能将这两种方法混为一谈。
使用ServiceHost 或a WebHttpBinding解决此问题。
此外,请注意,对于WCF REST样式绑定,您需要确保IIS可以支持更大的传输-默认情况下是4096 (4MB)
检查你的web.config --你有这样的条目吗?
<system.web>
......
<httpRuntime maxRequestLength="32678"/>
......
</system.web>即使我设置了binding对象的MaxReceivedMessageSize,我仍然得到“400Bad Request”
这只是一个“糟糕的请求”。检查WCF服务器日志以获取确切的错误消息。
https://stackoverflow.com/questions/17022573
复制相似问题