【问题标题】:HttpRequest POST method timeout when using proxy使用代理时 HttpRequest POST 方法超时
【发布时间】:2012-07-04 09:40:37
【问题描述】:

以下是我的代码:

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Timeout = timeout;
request.ReadWriteTimeout = timeout;
request.Proxy = new WebProxy("http://" + proxyUsed + "/", true);
request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.7 (KHTML, like Gecko) Chrome/16.0.912.63 Safari/535.7";
request.CookieContainer = new CookieContainer();
request.AllowAutoRedirect = true;

ASCIIEncoding encoding = new ASCIIEncoding();
string postData = "";

foreach (string[] p in parameter)
{
    if (postData != "")
        postData += "&";
    postData += string.Format("{0}={1}", p[0], p[1]);
}
byte[] data = encoding.GetBytes(postData);

request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = data.Length;

using (Stream newStream = request.GetRequestStream())
{
    newStream.Write(data, 0, data.Length);
    newStream.Close();
}

using (WebResponse myResponse = request.GetResponse())
{
    using (StreamReader sr = new StreamReader(myResponse.GetResponseStream(), System.Text.Encoding.UTF8))
    {
        result = sr.ReadToEnd();
        httpLink = myResponse.ResponseUri.AbsoluteUri;
        sr.Close();
    }
    myResponse.Close();
}

我在向网页发出 POST http 请求时遇到了问题。当我使用直接连接时,它工作正常。但是如果我使用代理,它会抛出一个超时异常。但是,如果我将 postData 设置为空,即使我使用代理也可以正常工作。 最可能的原因是什么? 谢谢。

【问题讨论】:

    标签: c# post proxy timeout httprequest


    【解决方案1】:

    没有大量信息可以继续,您是否有权访问代理服务器日志?

    如果您检查返回变量,您会收到 405 错误吗?这表明代理不支持 POST 处理。

    在调试方面,我会做setup fiddler作为您的代理(或透明使用它)然后您可以查看进出您的盒子的流量并分析数据以查看是否有任何异常请求/响应。

    【讨论】:

    • 我相信代理支持 POST 处理,因为它可以通过 POST 成功获取其他网站的响应
    • 我认为设置提琴手是你最好的选择,这样你就可以看到传入/传出的数据响应。
    • 啊..我不知道如何为VS程序设置fiddler,我试图只关注程序或所有进程,但我什么也没看到。
    • 安装 fiddler,将 fiddler 端口设置为 8888,在 fiddler 中为您的外部代理设置一个转发代理,并将您的代码代理指向 localhost:8888。
    • 对不起,如何在 fiddler 中设置转发代理?是把代码 oSession.oRequest["Proxy-Authorization"] = "xxx.xxx.xxx.xxx:xx";?我试过了,但好像不是这样
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-15
    • 2016-03-08
    • 2021-02-27
    • 1970-01-01
    相关资源
    最近更新 更多