如何避免服务器错误414非常长的QueryString值

时间:2010-05-28 03:21:18

标签: c# webrequest

我有一个项目需要向网页发布一个250万字符的QueryString。服务器本身只解析了5,400个字符或更少的URI。在为WebRequest / WebResponse,WebClient和Sockets尝试了几组不同的代码之后,我终于找到了解决我问题的以下代码:

HttpWebRequest webReq;
HttpWebResponse webResp = null;
string Response = "";
Stream reqStream = null;
webReq = (HttpWebRequest)WebRequest.Create(strURL);

Byte[] bytes = Encoding.UTF8.GetBytes("xml_doc=" + HttpUtility.UrlEncode(strQueryString));
webReq.ContentType = "application/x-www-form-urlencoded";
webReq.Method = "POST";
webReq.ContentLength = bytes.Length;
reqStream = webReq.GetRequestStream();
reqStream.Write(bytes, 0, bytes.Length);
reqStream.Close();
webResp = (HttpWebResponse)webReq.GetResponse();

if (webResp.StatusCode == HttpStatusCode.OK)
{
     StreamReader loResponseStream = new StreamReader(webResp.GetResponseStream(), Encoding.UTF8);
     Response = loResponseStream.ReadToEnd();
}

webResp.Close();
reqStream = null;
webResp = null;
webReq = null;

1 个答案:

答案 0 :(得分:1)

您无需手动使用套接字。您可以使用HttpWebRequest对象来处理套接字层。您可以在this other question上查看将发布文件的示例代码的答案。只需将目标网址更改为HTTPS网址即可使用SSL。