我正在创建一个 C#/.NET 应用程序以自动重新连接到 wifi 热点。互联网访问检测工作正常,但我无法重新连接。wifi 热点使用 HTTPS POST 登录表单。
我尝试了 stackoverflow 的几个建议,但到目前为止没有任何效果:我从我的请求中得到的答案是登录页面,我没有连接到 Internet。
我试过了:
public class Hotspot
{
public string Url { get; set; }
public string Method { get; set; }
public string Inputs { get; set; }
public HttpStatusCode Connect()
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(this.Url);
request.KeepAlive = false;
request.ProtocolVersion = HttpVersion.Version10;
request.Method = this.Method;
ServicePointManager.ServerCertificateValidationCallback += new RemoteCertificateValidationCallback(delegate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) { return true; });
byte[] post_bytes = Encoding.ASCII.GetBytes(this.Inputs);
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = post_bytes.Length;
using (Stream requestStream = request.GetRequestStream())
{
requestStream.Write(post_bytes, 0, post_bytes.Length);
}
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Console.WriteLine(new StreamReader(response.GetResponseStream()).ReadToEnd());
Console.WriteLine(response.StatusCode);
return response.StatusCode;
}
public void Connect2()
{
using (WebClient client = new WebClient())
{
client.Headers.Add(HttpRequestHeader.ContentType, "text/xml");
System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
byte[] response = client.UploadData(this.Url, "POST", encoding.GetBytes(this.Inputs));
Console.WriteLine(encoding.GetString(response));
}
}
}
- this.Url 看起来像 @"https://hotspot.somtehining.com/nb4_crypt.php"
- this.Method 是“POST”
- this.Inputs 是一个字符串,表示所有输入及其值(如 @"username=me&password=something&conditions=on&blablah=")
两种方法都输出登录页面,我仍然没有连接到互联网。第一种方法(连接)返回 200。我检查了输入值,它们是正确的:我可以使用 Web 浏览器进行连接。