フォーム送信時にスクリプトを呼び出し、HTTP POSTまたはGETを使用してパラメータを渡すサイトが多数あり、Webデバッガを使用してパラメータが渡されています。今私はC#の私のWindowsアプリケーションを通して同じことをしたい。どのように私はそのような機能を達成することができますか?CでWindowsアプリケーションを使用してhttpメソッドを実行する#
私は現在、C#でHttpWebRequestクラスとHttpWebResponseクラスを使用しています。しかし、私はロードして作業しようとする各ページの明示的なコードを書く必要があるので、それは痛みです。私は例として、ユーザー名とパスワードをPHPページに渡して、レスポンスを取得しようとしています。このレスポンスは、ユーザーがログインしているかどうかを識別するCookieとページを返します。
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");
}
これは
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;
}
私createreqクラスである私は、正しい軌道に乗っていますか?それを行うためのより良い方法はありますか?
あなたはWebClientクラスで見たことがありますか? HttpWebRequest/Responseクラスに含まれる多くの配管工事を包括しています –