如何以编程方式登录到一个网站,不返回使用C#的HTML响应?使用HttpWebRequest的尝试,但它挂起当我打电话的GetResponse() [英] How do I programmatically login into a website that does not return an html response using c#? Tried using HttpWebRequest but it hangs when I call GetResponse()

查看:124
本文介绍了如何以编程方式登录到一个网站,不返回使用C#的HTML响应?使用HttpWebRequest的尝试,但它挂起当我打电话的GetResponse()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想以编程方式登录到使用C#网站(http://www.smscountry.com),但该程序挂在第二的GetResponse()的要求,在那里我试图通过POST方法和填充登陆表单字段。

I am trying to programmatically login into a website ("http://www.smscountry.com") using C#, but the program hangs at the second GetResponse() request, where I try to login by POST method and filling the form fields.

        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的响应,但我不能完全肯定。有没有办法解决这个问题?或者有没有其他的方法可以让我登录到这个特定的网站?

I don't understand what the problem is. I think it might have something to do with the page getting redirected an not returning an html response, but I am not completely sure. Is there a solution to this problem? Or is there any other way I can login into this particular website?

推荐答案

您需要发送数据之前,你可以得到你的回应。伊戈尔看到你设置请求的内容长度,但而不是设置内容长度为0,你需要发送内容到服务器:

You need to send your data before you can get your response. Igor saw you were setting the content length of the request but rather than set the content length to 0, you need to send your content to the server:

...
webReq.GetRequestStream().write( data, 0, data.Length );
webReq.GetRequestStream().close();
webResp = (HttpWebResponse)webReq.GetResponse();
responseString = new StreamReader(webResp.GetResponseStream()).ReadToEnd();
...

你没有得到回应的原因是因为服务器正在等待您发送data.Length字节数。

The reason you're not getting a response is because the server is waiting for you to send data.Length number of bytes.

这篇关于如何以编程方式登录到一个网站,不返回使用C#的HTML响应?使用HttpWebRequest的尝试,但它挂起当我打电话的GetResponse()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆