如果内容长度> 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 {https://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%system32inetsrvappcmd set config -section:requestFiltering -requestLimits.maxAllowedContentLength:1000000
或者如果您只想为您的应用设置它:
%windir%system32inetsrvappcmd set config "Default Web Site/" -section:requestFiltering -requestLimits.maxAllowedContentLength:1000000
您可以在此处找到更多限制选项。
希望这可以帮助
您可能必须在web.config中启用大文件支持(httpRuntime,maxRequestLength参数)。
这是一个样本
上述就是C#学习教程:如果内容长度> 7kb使用C#,则无法在WebRequest上发布分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/cdevelopment/1012839.html