我在C#中有这段代码:
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.ContentType = "application/x-www-form-urlencoded";
request.Timeout = 30000;
request.Method = "POST";
request.KeepAlive = true;
request.AllowAutoRedirect = false;
Stream newStream = request.GetRequestStream();
newStream.Write(bPostData, 0, bPostData.Length);
byte[] buf = new byte[1025]; int read = 0; string sResp = "";
HttpWebResponse wResp = (HttpWebResponse)request.GetResponse();
Stream resp = wResp.GetResponseStream();
行HttpWebResponse wResp =...
只是挂起(因为没有来自URL的响应)。我不确定它到底崩溃的原因(因为我甚至没有得到异常错误)。我在IE中测试了URL,它运行正常。我还检查了bPostData,其中有一个数据。
哪里出错了?
答案 0 :(得分:3)
尝试在变量newStream中关闭请求流。也许API等待它完成。
答案 1 :(得分:1)
你必须增加限制:
ServicePointManager.DefaultConnectionLimit = 10; // Max number of requests
答案 2 :(得分:0)
尝试简化代码并伪造用户代理。也许该网站阻止/限制刮刀/机器人。还要确保正确编码application/x-www-form-urlencoded
HTTP POST值。为此我建议你WebClient:
using (var client = new WebClient())
{
client.Headers[HttpRequestHeader.UserAgent] = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:2.0) Gecko/20100101 Firefox/4.0";
var values = new NameValueCollection
{
{ "param1", "value1" },
{ "param2", "value2" },
};
byte[] result = client.UploadValues(url, values);
}
答案 3 :(得分:0)
当我早些时候发表评论时,我已经在我的办公室运行了你的代码(严重防火墙)我得到了你所做的相同结果。回到家,再次尝试(减少防火墙)它工作得很好......我猜你在那里有一个障碍。我相信你正面临防火墙问题。
答案 4 :(得分:0)
使用content-length = 0
示例:
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(URL);
request.Method = "POST";
request.ContentLength = 0;
var requestStream = request.GetRequestStream();
HttpWebResponse res = (HttpWebResponse)request.GetResponse();
res.Close();