private void LoginButton_Click(object sender, EventArgs e)
{
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(loginUrl);
IAsyncResult result = request.BeginGetResponse(
new AsyncCallback(DeleResponse), request);
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
在此可以叫上按鈕單擊事件在異步Web請求中設置請求屬性失敗。 C#
private void DeleResponse(IAsyncResult result)
{
byte[] PostData = Encoding.UTF8.GetBytes("username=" + userInp.Text + "&password=" + passInp.Text + extraLoginPostString);
LoginButton.Text = "Logging in...";
LoginButton.Enabled = false;
HttpWebRequest request = (HttpWebRequest)result.AsyncState;
request.Method = "Post";
request.CookieContainer = authCookie;
request.ContentType = "application/x-www-form-urlencoded";
request.AllowAutoRedirect = false;
postWriter = request.GetRequestStream();
postWriter.Write(PostData, 0, PostData.Length);
postWriter.Close();
HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(result);
string serverData = new StreamReader(response.GetResponseStream()).ReadToEnd();
string loginValidateString = response.GetResponseHeader(loginValidateStringHolder);
if (loginValidateString.Contains(LoggedKeyword))
{
some process here:
}
else if(FAILKEYWORDCHECK HERE)
{
login page process here;
}
}
方法的問題是,當我檢查這個用小提琴手我只能看到下面的標題屬性。
Connection: Keep-Alive;
Host: www.example.com
什麼是我不能在請求頭中設置屬性的原因?
編輯:增加了我已經實現的同步請求方法,沒有任何錯誤。
private void LoginButton_Click(object sender, EventArgs e)
{
try
{
LoginButton.Text = "Logging in...";
LoginButton.Enabled = false;
byte[] PostData = Encoding.UTF8.GetBytes("username=" + userInp.Text + "&password=" + passInp.Text + extraLoginPostString);
request = (HttpWebRequest)WebRequest.Create(loginUrl);
request.Method = "Post";
request.CookieContainer = authCookie;
request.ContentType = "application/x-www-form-urlencoded";
request.AllowAutoRedirect = false;
postWriter = request.GetRequestStream();
postWriter.Write(PostData, 0, PostData.Length);
postWriter.Close();
response = (HttpWebResponse)request.GetResponse();
string serverData = new StreamReader(response.GetResponseStream()).ReadToEnd();
string loginValidateString = response.GetResponseHeader(loginValidateStringHolder);
if (loginValidateString.Contains(LoggedKeyword))
{
MessageBox.Show("Logged in Successfully");
foreach (Cookie cookieReader in response.Cookies)
{
authCookie.Add(cookieReader);
}
Success method continues..
}
else if (loginValidateString.Contains(failedLogKeyword))
{
Failed process
}
}
catch
{
Catchblock
}
}
手段,我只是知道如何設置正常請求的屬性。
請問這可以解決你嘗試查看MSDN網站http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest%28v=vs.95%29.aspx – MethodMan 2011-12-26 16:18:09
究竟是什麼你試圖達到?如果你需要設置標題,你可以通過header屬性來完成它:http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.headers.aspx – TheBoyan 2011-12-26 16:19:17
我會假設SMS提供商已採取措施防止用戶自動發送短信垃圾郵件。聯繫您的提供商尋求支持。 – 2011-12-26 16:26:19