执行使用Windows应用程序在C#中的HTTP方法 [英] performing http methods using windows application in c#

查看:130
本文介绍了执行使用Windows应用程序在C#中的HTTP方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有许多网站其中呼吁表单的脚本提交,并使用HTTP POST或GET的参数传递,使用Web调试我已经找到被传递的参数。现在,我希望通过在C#我的Windows应用程序做同样的事情。我怎样才能实现这样的功能呢?

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?

我目前使用C#HttpWebRequest和HttpWebResponse类。但它是一种痛苦,因为我有写显式代码的每一页我尝试加载和工作。例如我正在尝试的用户名和密码传递给PHP页面,并采取响应,这将发出一个cookie并在返回一个页面,在此基础上我确定如果用户已登录或没有。

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");
                }

这是我的createreq类

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?

推荐答案

您应该使用的 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.

还有的MSDN页面上一个简单的例子,但一些示例代码使用它可能看起来像:

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
}

这篇关于执行使用Windows应用程序在C#中的HTTP方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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