[英]HttpWebRequest GetResponse() not returning anything
我已经编写了一个代码来调用返回一些数据的网站。 我已经使用了HttpWebRequest.GetResponse()
方法。 当我在浏览器中点击URL时,它将返回数据。 但是在我的C#代码中,有时返回数据,有时不返回任何内容。
该请求不会引发任何错误,例如超时或访问被拒绝。 它什么也不返回。 如果我在代码中使用调试器,它将返回数据。
代码如下;
HttpWebRequest clnt = (HttpWebRequest)HttpWebRequest.Create(restURL);
var resp = clnt.GetResponse();
if ((resp.ContentLength > 0))
{
using (System.IO.StreamReader str = new System.IO.StreamReader(resp.GetResponseStream()))
{
if (str != null)
{
string response = str.ReadToEnd();
str.Close();
return response;
}
}
}
如果我有任何遗漏,请帮助我。
您是否尝试过提供方法和内容类型?
clnt.Method = "POST";
clnt.ContentType = "application/x-www-form-urlencoded";
它会像这样:
HttpWebRequest httpWReq = (HttpWebRequest)WebRequest.Create(url);
httpWReq.Method = "POST";
httpWReq.ContentType = "application/x-www-form-urlencoded";
HttpWebResponse response = (HttpWebResponse)httpWReq.GetResponse();
string responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
return responseString;
希望这对您有所帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.