如果内容长度> 7kb使用C#,则无法在WebRequest上发布

我正在尝试将Json发布到我们的Web服务。 我的问题是,当我的request.ContentLength超过7kb时。 我将在request.GetResponse()处获得Webexception500。 但是如果我的request.ContentLength低于7kb,我的post就成功发送了。

错误信息:

The remote server returned an error: (500) Internal Server Error. 

源代码:

 public static string JsonPost(string url, string method, string postData) { Uri address = new Uri(url + method); HttpWebRequest request = WebRequest.Create(address) as HttpWebRequest; request.Method = "POST"; request.ContentType = "application/json"; byte[] byteData = UTF8Encoding.UTF8.GetBytes(postData); request.ContentLength = byteData.Length; using (Stream postStream = request.GetRequestStream()) { postStream.Write(byteData, 0, byteData.Length); } try { using (HttpWebResponse response = request.GetResponse() as HttpWebResponse) { StreamReader reader = new StreamReader(response.GetResponseStream()); string JsonResponse = reader.ReadToEnd(); return JsonResponse; } } catch (WebException ex) { string message = ((System.Net.HttpWebResponse)(ex.Response)).StatusDescription; if (message.Contains("busy")) return message; else throw new Exception(message); } } 

Web.config文件:

                                                                       

exception日志:

  (System.Net.HttpWebResponse)(ex.Response) {System.Net.HttpWebResponse} System.Net.HttpWebResponse base {System.Net.HttpWebResponse} System.Net.WebResponse {System.Net.HttpWebResponse} CharacterSet "ISO-8859-1" string ContentEncoding "" string ContentLength 1047 long ContentType "text/html" string Cookies {System.Net.CookieCollection} System.Net.CookieCollection Headers { Access-Control-Allow-Origin: * Content-Length: 1047 Cache-Control: private Content-Type: text/html Date: Tue, 18 Sep 2012 22:33:36 GMT Set-Cookie: ASP.NET_SessionId=belurseo1i0ureppkspetw1a; path=/; HttpOnly Server: Microsoft-IIS/7.0 X-AspNet-Version: 4.0.30319 X-Powered-By: ASP.NET } System.Net.WebHeaderCollection IsMutuallyAuthenticated false bool LastModified {9/19/2012 6:34:07 AM} System.DateTime Method "POST" string ProtocolVersion {1.1} System.Version ResponseUri {http://localhost/service.svc/Update} System.Uri Server "Microsoft-IIS/7.0" string StatusCode InternalServerError System.Net.HttpStatusCode StatusDescription "The server encountered an error processing the request. Please see the server logs for more details." string SupportsHeaders true bool 

试试这个:

 %windir%\system32\inetsrv\appcmd set config -section:requestFiltering -requestLimits.maxAllowedContentLength:1000000 

或者如果您只想为您的应用设置它:

 %windir%\system32\inetsrv\appcmd set config "Default Web Site/" -section:requestFiltering -requestLimits.maxAllowedContentLength:1000000 

您可以在此处找到更多限制选项。

希望这可以帮助

您可能必须在web.config中启用大文件支持(httpRuntime,maxRequestLength参数)。
这是一个样本