C# HttpWebRequest 到 HTTPS 失败
我正在尝试以编程方式登录此网站 https://www.virginmobile.com.au(在右侧有一个会员登录表格)。
该表格有效。但是,当我对表单操作执行 POST 请求时 (https://www. virginmobile.com.au/selfcare/MyAccount/LogoutLoginPre.jsp)失败了。
它返回 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.
}
}
有人可以帮助我吗?提前致谢!
I am trying to login to this website https://www.virginmobile.com.au programatically (on the right there is a Member Login form).
That form works. But when I do a POST request to the form action (https://www.virginmobile.com.au/selfcare/MyAccount/LogoutLoginPre.jsp) it failed.
It returns a 302, then following up to the new location, it returns 405.
This is my code 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.
}
}
Could anyone help me? Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
302 响应尝试将您重定向到另一个页面,因此问题可能是您的 POST 数据没有发送到重定向的页面。
也许尝试设置 HttpWebRequest.AllowAutoRedirect = false 并捕获您得到的异常
后退。然后创建另一个对重定向 URL(在 Location 响应标头中指定)的请求,然后使用相同的 POST 数据再次发出请求。
The 302 response is trying to redirect you to another page, so the problem might be that your POST data isn't being sent to the redirected page.
Maybe try setting
HttpWebRequest.AllowAutoRedirect = false
and catch the exception that you getback. Then create another request to the redirected URL (specified in the Location response header) and then issue the request again with the same POST data.
您随请求发送的标头很少。他们编写的脚本很可能期望存在某些标头。我能立即想到的标头是:
User-Agent
(标识您的浏览器和版本;例如,您可以假装是 Firefox)Referer
(标识您来自的 URL;将主页 URL 放在这里)Accept-Charset
、Accept-Encoding
、Accept-Language
但可能有其他的。您可能可以使用您提到的 Fiddler 工具来找出 Firefox(或您使用的任何浏览器)通过正常(非 HTTPS)请求发送的标头,然后将其中一些标头添加到您的请求中,看看是否可以正常工作。 (就我个人而言,我使用 TamperData 来实现此目的,这是一个火狐浏览器插件。)
You are sending pretty few headers with your request. It is very possible that they wrote their script so that it expects certain headers to be present. Headers that I can think of off the top of my head are:
User-Agent
(identifies your browser and version; you can pretend to be Firefox, for example)Referer
(identifies the URL you came from; put the homepage URL in here)Accept-Charset
,Accept-Encoding
,Accept-Language
but there may be others. You can probably use the Fiddler tool you mentioned to find out what headers Firefox (or whatever browser you’re using) sends with normal (non-HTTPS) requests and then add some of them to your request and see whether that makes it work. (Personally, I use TamperData for this purpose, which is a Firefox plugin.)
我收到 404 错误 - 远程服务器返回错误:(404) 未找到。下面是我在收到 405 错误的同一行代码中收到错误的代码。如果我将代码替换为您以前的版本,则不会返回 404,但会返回 405 错误。
谢谢
I'm getting a 404 error - The remote server returned an error: (404) Not Found. Below is the code I'm getting the error at the same line of code as you were getting the 405 error. If I replace the code with your previous version no 404 is returned but the 405 error is returned.
Thanks
已解决:405 是因为我发送的是 POST 而不是 GET
Resolved: 405 was because I was sending a POST instead of a GET