当我将发布数据发送到代理服务器时 - 它会返回错误:
尝试处理请求时遇到无效请求错误:
一些可能的问题是:
遗失或未知 请求方法。
缺少URL。
缺少HTTP标识符 (HTTP / 1.0)。
请求太大。
内容长度缺失 POST或PUT请求。
主机名中的非法字符;下划线 不允许。
HTTP / 1.1 Expect:正在询问功能 HTTP / 1.0软件。
和标题:
/post.php?tid=19 HTTP/1.1
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:28.0) Gecko/20100101 Firefox/28.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Content-Type: application/x-www-form-urlencoded
Referer: http://site.ru/viewtopic.php?pid=51
Host: site.ru
Content-Length: 552
Expect: 100-continue
Proxy-Connection: Keep-Alive
在程序中设置代理:
request = (HttpWebRequest)HttpWebRequest.Create(postUrl);
request.Proxy = new WebProxy( this.ip + ":" + this.port);
request.UseDefaultCredentials = true;
发送请求:
try
{
System.Net.ServicePointManager.Expect100Continue = false;
Stream dataStream = request.GetRequestStream();
dataStream.Write(data, 0, data.Length);
dataStream.Close();
WebResponse response = request.GetResponse();
Console.WriteLine(((HttpWebResponse)response).StatusDescription);
dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
string responseFromServer = reader.ReadToEnd();
Console.WriteLine(responseFromServer);
reader.Close();
dataStream.Close();
response.Close();
}
catch (WebException ex)
{
Console.WriteLine(ex.Status);
if (ex.Response != null)
{
if (ex.Response.ContentLength != 0)
{
using (var stream = ex.Response.GetResponseStream())
{
using (var reader = new StreamReader(stream))
{
Console.WriteLine(reader.ReadToEnd());
}
}
}
}
}
当代理服务器被禁用或注释掉代码发送帖子数据时 - 请求成功
// Stream dataStream = request.GetRequestStream();
//dataStream.Write(data,0,data.Length);
//dataStream.Close();
感谢您的回答!
答案 0 :(得分:0)
关于缺少HTTP标识符的评论
request.Proxy = new WebProxy("http://" + this.ip + ":" + this.port);
有什么不同吗?