我正在创建一个C#/ .NET应用程序来自动重新连接到wifi热点。 Internet访问检测工作正常,但我无法重新连接。 wifi热点使用HTTPS POST登录表单。
我尝试了stackoverflow的几个建议,但到目前为止没有任何工作:我从我的请求得到的答案是登录页面,我没有连接到互联网。
我试过了:
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));
}
}
}
两种方法都输出登录页面,但我仍未连接到Internet。第一种方法(Connect)返回200。 我检查了输入值,它们是正确的:我可以使用网络浏览器进行连接。