performing http methods using windows application

2019-02-27 05:26发布

问题:

There are many sites which call a script upon form submit and pass in parameters using HTTP POST or GET, using a web debugger i have found the parameters being passed. Now i wish to do the same thing through my Windows Application in C#. How can i achieve such a functionality?

I am currently using HttpWebRequest and HttpWebResponse class in C#. But it is a pain as i have to write explicit code for each page i try to load and work. For Example i am trying to pass username and password to a php page and taking the response, which will send a cookie and a page in return, based on which i identify if the user has logged in or not.

HttpWebRequest loginreq = createreq("http://www.indyarocks.com/mobile/index.php");
                String logintext = "username=" + TxtUsrname.Text + "&pass=" + TxtPasswd.Password + "&button.x=0&button.y=0";
                loginreq.ContentLength = logintext.Length;
                StreamWriter writerequest = new StreamWriter(loginreq.GetRequestStream());
                writerequest.Write(logintext);
                writerequest.Close();
                HttpWebResponse getloginpageresponse = (HttpWebResponse)loginreq.GetResponse();
                cookie = getloginpageresponse.Cookies[0];
                BinaryFormatter bf1 = new BinaryFormatter();
                Stream f1 = new FileStream("E:\\cookie.dat", FileMode.OpenOrCreate);
                bf1.Serialize(f1, cookie);
                f1.Close();

                string nexturl = getloginpageresponse.Headers[HttpResponseHeader.Location];
                StreamReader readresponse = new StreamReader(getloginpageresponse.GetResponseStream());
                if (nexturl == "p_mprofile.php")
                {
                    MessageBox.Show("Login Successful");
                    GrpMsg.IsEnabled = true;
                }
                else if (nexturl == "index.php?msg=1")
                {
                    MessageBox.Show("Invalid Credentials Login again");
                }

This is my createreq class

 private HttpWebRequest createreq(string url)
        {
            HttpWebRequest temp = (HttpWebRequest)WebRequest.Create(url);
            temp.Method = "POST";
            temp.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; SLCC1; .NET CLR 2.0.50727; Media Center PC 5.0; .NET CLR 3.0.04506; .NET CLR 3.5.21022; FDM)";
            temp.KeepAlive = true;
            temp.ContentType = "application/x-www-form-urlencoded";
            temp.CookieContainer = new CookieContainer();
            temp.AllowAutoRedirect = false;
            return temp;
        }

Am i on the right track? Is there any better way to do it?

回答1:

You should use System.Net.WebClient.

You can use it to make a request with any method and headers that you'd like, and get the resulting page with a simple stream read.

There's a simple example on the MSDN page, but some sample code for using it might look like:

WebClient webclient= new WebClient();

using (StreamReader reader = new StreamReader(webclient.OpenRead("http://www.google.com")))
{
        string result = reader.ReadToEnd();
         // Parse web page here
}