在我进入代码之前,让我解释一下我正在尝试做什么。这是一个针对API的简单清单,api有一个限制,我想方设法通过我的路由器轻松更改我的IP来打破限制。它在大多数情况下都很好用,但在重新连接成功之后的某个时刻,工作人员停止了,我必须再次点击该按钮。
这是代码,首先是Form类:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace MyNameChecker
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void chk_Click(object sender, EventArgs e)
{
if (!checkNames.IsBusy)
{
checkNames.RunWorkerAsync();
}
}
int last = 0;
private void checkNames_DoWork(object sender, DoWorkEventArgs e)
{
string namesList = "";
string[] namesArray;
if (names.InvokeRequired) names.Invoke(new MethodInvoker(delegate { namesList = names.Text; }));
else namesList = names.Text;
if (!string.IsNullOrEmpty(namesList))
{
namesArray = namesList.Replace("\r","").Split('\n');
while (last < namesArray.Length)
{
string tempStr = NameChecker.check(namesArray[last].Replace("\r", "").Replace("\n", ""));
if (tempStr == "available")
{
using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"C:\Names\available.txt", true)) file.WriteLine(namesArray[last]);
last++;
}
else if (tempStr == "unavailable" || tempStr == "skep")
{
last++;
}
else
{
while(!NameChecker.reconnect());
}
}
if (last >= namesArray.Length) last = 0;
}
}
}
}
第二个MyNameChecker类:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Windows.Forms;
namespace MyNameChecker
{
class NameChecker
{
public static string check(string name)
{
if (string.IsNullOrEmpty(name)) return "skep";
string url = "https://example.com/api";
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
webRequest.AllowAutoRedirect = false;
webRequest.Method = "POST";
webRequest.KeepAlive = true;
webRequest.ContentType = "application/json; charset=UTF-8";
ASCIIEncoding encoding = new ASCIIEncoding();
string stringData = "{\"name\":\"" + name + "\"}";
byte[] data = encoding.GetBytes(stringData);
webRequest.ContentLength = data.Length;
webRequest.GetRequestStream().Write(data, 0, data.Length);
webRequest.Timeout = 60000;
try
{
// Get the response ...
using (HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse())
{
if (string.IsNullOrEmpty(webResponse.Headers["RequestNum"])) return "retry";
else return status(webResponse);
}
}
catch (WebException ex)
{
var res = (HttpWebResponse)ex.Response;
if (string.IsNullOrEmpty(res.Headers["RequestNum"])) return "retry";
else return status(res);
}
catch (Exception ex)
{
using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"C:\Names\Exceptions.txt", true)) file.WriteLine(ex.Message);
return "retry";
}
return "retry";
}
private static string status(HttpWebResponse res)
{
switch((int)res.StatusCode)
{
case 201:
return "available";
case 400:
return "unavailable";
default:
return "retry";
}
}
public static bool reconnect()
{
string url = "http://router.lan/cgi/b/is/_pppoe_/ov/?be=0&l0=1&l1=1&name=Internet";
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
webRequest.AllowAutoRedirect = false;
webRequest.Method = "POST";
webRequest.ContentType = "application/x-www-form-urlencoded";
webRequest.KeepAlive = true;
System.Net.ServicePointManager.Expect100Continue = false;
ASCIIEncoding encoding = new ASCIIEncoding();
string stringData = "0=12&1=Internet&32=1";
byte[] data = encoding.GetBytes(stringData);
webRequest.ContentLength = data.Length;
webRequest.GetRequestStream().Write(data, 0, data.Length);
webRequest.Timeout = 25000;
try
{
// Get the response ...
using (HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse())
{
// Get the body stream
string body = new System.IO.StreamReader(webResponse.GetResponseStream()).ReadToEnd();
if (body.Contains("Uptime:")) return true;
return false;
}
return false;
}
catch (WebException ex)
{
var res = (HttpWebResponse)ex.Response;
string body = new System.IO.StreamReader(res.GetResponseStream()).ReadToEnd();
if (body.Contains("Uptime:")) return true;
return false;
}
catch (Exception ex)
{
using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"C:\Names\Exceptions.txt", true)) file.WriteLine(ex.Message);
return false;
}
return false;
}
}
}
我尝试了所有可以通过增加超时来捕获所有异常但是没有记录异常?我无法解决这个问题。通常我会去睡觉,让它做它的工作,但这只是没有按预期工作。
BTW我用fiddler调试请求,并且有一个奇怪的标题Expect: 100-continue
我不知道这是否与破坏有关但我认为最好提一下。