我正嘗試以編程方式登錄本網站https://www.virginmobile.com.au(右側有一個會員登錄表單)。C#HttpWebRequest HTTPS失敗
該表單有效。但是,當我對錶單操作(https://www.virginmobile.com.au/selfcare/MyAccount/LogoutLoginPre.jsp)發出POST請求時,它失敗了。
它返回一個302,然後跟進到新的位置,它會返回405
這是我的代碼test1.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.Text;
using System.IO;
using System.Security.Cryptography.X509Certificates;
using System.Net;
public partial class test1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string uri = "https://www.virginmobile.com.au/selfcare/MyAccount/LogoutLoginPre.jsp";
string parameters = "username=0411222333&password=123";
System.Net.ServicePointManager.CertificatePolicy = new MyPolicy();
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(uri);
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
//req.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.2) Gecko/20100316 Firefox/3.6.2 (.NET CLR 3.0.4506.2152)";
//req.Referer = "http://www.virginmobile.com.au/";
//req.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
req.AllowAutoRedirect = false;
// Send the Post
byte[] paramBytes = Encoding.ASCII.GetBytes(parameters);
req.ContentLength = paramBytes.Length;
Stream reqStream = req.GetRequestStream();
reqStream.Write(paramBytes, 0, paramBytes.Length); //Send it
reqStream.Close();
// Get the response
HttpWebResponse response = (HttpWebResponse)req.GetResponse();
if (response == null) throw new Exception("Response is null");
if (!string.IsNullOrEmpty(response.Headers["Location"]))
{
string newLocation = response.Headers["Location"];
// Request the new location
req = (HttpWebRequest)WebRequest.Create(newLocation);
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
//req.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.2) Gecko/20100316 Firefox/3.6.2 (.NET CLR 3.0.4506.2152)";
//req.Referer = "http://www.virginmobile.com.au/";
//req.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
req.AllowAutoRedirect = false;
req.CookieContainer = new CookieContainer();
req.CookieContainer.Add(response.Cookies);
// Send the Post
paramBytes = Encoding.ASCII.GetBytes(parameters);
req.ContentLength = paramBytes.Length;
reqStream = req.GetRequestStream();
reqStream.Write(paramBytes, 0, paramBytes.Length); //Send it
reqStream.Close();
// Get the response
response = (HttpWebResponse)req.GetResponse(); //**** 405 Method Not Allowed here
}
StreamReader sr = new StreamReader(response.GetResponseStream());
string responseHtml = sr.ReadToEnd().Trim();
Response.Write(responseHtml);
}
}
public class MyPolicy : ICertificatePolicy
{
public bool CheckValidationResult(ServicePoint srvPoint, X509Certificate certificate, WebRequest request, int certificateProblem)
{
return true; // Return true to force the certificate to be accepted.
}
}
誰能幫助我?提前致謝!
使用TamperData,我發現在virginmobile.com.au真正形成還重定向...但我想試試。謝謝! – Aximili 2010-08-20 01:59:47
它似乎設置了一個cookie。你如何獲得使用C#的「位置」? – Aximili 2010-08-20 02:15:07
調用GetResponse()後,您可以在WebResponse.Headers屬性中找到響應標頭。您可以使用'request.CookieContainer = new CookieContainer();'來捕獲cookie。 – 2010-08-20 02:18:31