Я пытаюсь программно войти на сайт ("http://www.smscountry.com") с использованием C #, но программа зависает при втором запросе GetResponse (), где я пытаюсь войти методом POST и заполняю поля формы.
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-ответа, но я не совсем уверен. Есть ли решение этой проблемы? Или я могу как-нибудь войти на этот конкретный сайт?