如何使用c#以编程方式登录到不返回html响应的网站?它在调用GetResponse()时挂起
我试图使用C#以编程方式登录到一个网站(“”),但程序在第二个GetResponse()请求时挂起,我尝试通过POST方法登录并填充表单字段如何使用c#以编程方式登录到不返回html响应的网站?它在调用GetResponse()时挂起,c#,.net,login,httpwebrequest,C#,.net,Login,Httpwebrequest,我试图使用C#以编程方式登录到一个网站(“”),但程序在第二个GetResponse()请求时挂起,我尝试通过POST方法登录并填充表单字段 public String GetWebContent(String username, String password) { //The URL of the Login Form of the website String urlSignin = "http://www.smscount
public String GetWebContent(String username, String password)
{
//The URL of the Login Form of the website
String urlSignin = "http://www.smscountry.com/";
//The action URL of the Login Form of the website on Submit
String urlLogin = "http://www.smscountry.com/LoginCheck.asp?msg=";
//Initializes the Uri object of the URLs
Uri uriSignin = new Uri(urlSignin);
Uri uriLogin = new Uri(urlLogin);
//Hashtable to store the form details
Hashtable formData = new Hashtable();
formData.Add("UTC", new Hashtable());
formData.Add("txt_Username", new Hashtable());
formData.Add("txt_Password", new Hashtable());
((Hashtable)formData["UTC"])["value"] = -330;
((Hashtable)formData["txt_Username"])["value"] = username;
((Hashtable)formData["txt_Password"])["value"] = password;
//Initializing the data for the post action
String postData = "";
foreach (string name in formData.Keys)
{
postData += "&" + name + "=" + ((Hashtable)formData[name])["value"];
}
postData = postData.Substring(1);
System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
byte[] data = encoding.GetBytes(postData);
HttpWebRequest webReq;
HttpWebResponse webResp;
//To store the cookies of the response objects to be used for the next request
CookieContainer cookies = new CookieContainer();
String responseString = "";
try
{
//Getting response for the Signin page
webReq = (HttpWebRequest)WebRequest.Create(urlSignin);
webResp = (HttpWebResponse)webReq.GetResponse();
//Storing response cookies to be used in the next request
cookies.Add(webResp.Cookies);
//Storing ASPSESSION cookie that appears in the Response header Set-Cookie to be used in the next request
string sessionCookie = webResp.Headers["Set-Cookie"];
responseString = new StreamReader(webResp.GetResponseStream()).ReadToEnd();
string respCookie = sessionCookie.Substring(0, sessionCookie.IndexOf(';'));
char[] separator = {'='};
string[] cookieValues = respCookie.Split(separator);
cookies.Add(new Cookie(cookieValues[0], cookieValues[1], "/", "www.smscountry.com"));
//Initializing the request object for log in
webReq = (HttpWebRequest)WebRequest.Create(urlLogin);
webReq.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
webReq.Referer = urlSignin;
webReq.KeepAlive = true;
webReq.Method = "POST";
webReq.ContentType = "application/x-www-form-urlencoded";
webReq.ContentLength = data.Length;
webReq.AllowAutoRedirect = false;
webReq.CookieContainer = cookies;
webReq.Timeout = 30000;
webReq.ReadWriteTimeout = 60000;
//Get the response for the request to log in
//PROBLEM OCCURS HERE - THE CODE DOES NOT EXECUTE FURTHER
webResp = (HttpWebResponse)webReq.GetResponse();
responseString = new StreamReader(webResp.GetResponseStream()).ReadToEnd();
webResp.Close();
return responseString;
}
catch (Exception ex)
{
throw ex;
}
}
我不明白问题出在哪里。我认为这可能与页面被重定向以及没有返回html响应有关,但我不能完全确定。这个问题有解决办法吗?或者是否有其他方法可以登录此特定网站?问题出现在以下行:
webReq.ContentLength=data.Length
在这里,您将POST请求正文的长度设置为大于零,而实际正文为空;您要发送的只是标题。将该行替换为webReq.ContentLength=0代码>完成了。问题在下面一行:webReq.ContentLength=data.Length
在这里,您将POST请求正文的长度设置为大于零,而实际正文为空;您要发送的只是标题。将该行替换为webReq.ContentLength=0代码>完成。在收到回复之前,您需要发送数据。Igor看到您正在设置请求的内容长度,但您需要将内容发送到服务器,而不是将内容长度设置为0:
...
webReq.GetRequestStream().write( data, 0, data.Length );
webReq.GetRequestStream().close();
webResp = (HttpWebResponse)webReq.GetResponse();
responseString = new StreamReader(webResp.GetResponseStream()).ReadToEnd();
...
您没有收到响应的原因是服务器正在等待您发送数据。长度字节数。您需要先发送数据,然后才能收到响应。Igor看到您正在设置请求的内容长度,但您需要将内容发送到服务器,而不是将内容长度设置为0:
...
webReq.GetRequestStream().write( data, 0, data.Length );
webReq.GetRequestStream().close();
webResp = (HttpWebResponse)webReq.GetResponse();
responseString = new StreamReader(webResp.GetResponseStream()).ReadToEnd();
...
您没有收到响应的原因是服务器正在等待您发送数据。长度字节数。是的,这就是问题所在。谢谢是的,这就是问题所在。谢谢