C#:如何通过WebRequest发布大字符串?
如何使用POST上传一个大字符串(在我的例子中是带有BLOB的XML),而不使用GetResponse获得超时 更改超时有帮助,但这并不是真正的解决方案。 如果服务器真的死了,或者帖子被中断了,我必须等待非常大的超时 有什么想法吗C#:如何通过WebRequest发布大字符串?,c#,httpwebrequest,timeout,http-post,C#,Httpwebrequest,Timeout,Http Post,如何使用POST上传一个大字符串(在我的例子中是带有BLOB的XML),而不使用GetResponse获得超时 更改超时有帮助,但这并不是真正的解决方案。 如果服务器真的死了,或者帖子被中断了,我必须等待非常大的超时 有什么想法吗 HttpWebRequest webRequest = null; string response = ""; byte[] bytes = Encoding.UTF8.GetBytes(xml); try { webRequest = (HttpWebRe
HttpWebRequest webRequest = null;
string response = "";
byte[] bytes = Encoding.UTF8.GetBytes(xml);
try
{
webRequest = (HttpWebRequest)WebRequest.Create("http://" + this.host + ":" + this.port);
webRequest.ContentType = "application/x-www-form-urlencoded";
webRequest.Method = "POST";
webRequest.Timeout = 5000;
webRequest.ContentLength = bytes.Length;
using (Stream requeststream = webRequest.GetRequestStream())
{
requeststream.Write(bytes, 0, bytes.Length);
requeststream.Close();
}
using (HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse())
{
using (StreamReader sr = new StreamReader(webResponse.GetResponseStream()))
{
response = sr.ReadToEnd().Trim();
sr.Close();
}
webResponse.Close();
}
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
}
return response;
是的,这几乎是预期的http行为 选项:
- 有一个大的超时(您已经这样做了),并接受可能需要很长时间才能合法超时(而不是因为带宽而需要一段时间)
- 也许您可以对请求应用gzip(并告诉服务器您正在发送压缩的请求);老实说,我不知道这是否是自动支持的,但它肯定可以通过api显式地检查特定的头并对负载应用gzip解压缩来完成
- 更改api以执行一些小的上载和完成消息
- 接受它