我有一个项目,其中包含来自受密码保护的网站的数据抓取过程。数据抓取过程设置为石英调度程序作业,每小时触发一次。当我将项目部署到 web 时,该过程工作正常,但在 1 小时后,即下一个触发时间,它无法执行。
我确信石英调度程序的使用是正确的,因为可以毫无问题地完成另一项工作。我认为问题在于我不擅长的套接字问题。
这是代码:
using Quartz;
using SMSGonder;
using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
namespace TMiddaa.Quartz
{
public class Scout : IJob
{
static string baseUrl = "https://TargetWebSite.com";
static string email = "xx@yy.com";
static string password = "12345";
static string requesttype = "login";
static CookieContainer cookieContainer;
public void Execute(IJobExecutionContext context)
{
cookieContainer = new CookieContainer();
MakeWebRequest();
requesttype = "download";
MakeWebRequest();
}
public void MakeWebRequest()
{
StringBuilder postData = new StringBuilder();
string url = "";
string method = "GET";
if (requesttype == "login")
{
postData.Append(String.Format("user={0}&", email));
postData.Append(String.Format("password={0}&", password));
postData.Append(String.Format("type=&"));
postData.Append(String.Format("remember=1&"));
postData.Append(String.Format("captcha="));
method = "POST";
url = "/ajax/login.ajax.php";
}
else if (requesttype == "download")
{
url = "/somePage";
}
ASCIIEncoding ascii = new ASCIIEncoding();
byte[] postBytes = ascii.GetBytes(postData.ToString());
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(baseUrl + url);
request.ServicePoint.BindIPEndPointDelegate = new BindIPEndPoint(BindIPEndPointCallback);
request.Method = method;
if (method == "POST")
{
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = postBytes.Length;
}
request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";
request.CookieContainer = cookieContainer;
if (method == "POST")
{
Stream postStream = request.GetRequestStream();
postStream.Write(postBytes, 0, postBytes.Length);
var response = (HttpWebResponse)request.GetResponse();
var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
postStream.Flush();
postStream.Close();
}
else if (method == "GET")
{
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
//sayfanın htmlini responseString'ten alabilirsiniz.
var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
response.Close();
//Do stuff with responseString
}
}
public int m_LastBindPortUsed = 5000;
public IPEndPoint BindIPEndPointCallback(ServicePoint servicePoint, IPEndPoint remoteEndPoint, int retryCount)
{
int port = Interlocked.Increment(ref m_LastBindPortUsed); //increment
Interlocked.CompareExchange(ref m_LastBindPortUsed, 5001, 65534);
if (remoteEndPoint.AddressFamily == AddressFamily.InterNetwork)
{
return new IPEndPoint(IPAddress.Any, port);
}
else
{
return new IPEndPoint(IPAddress.IPv6Any, port);
}
}
}
}
我得到了创建此代码的帮助,因此我不太了解此代码的某些部分。我怀疑插座部分。如果有人能帮忙,我会很高兴。
Quartz包含一个记录器。您只需要插入它即可检索代码中可能发生的任何异常
如果您使用的是 .Net Core,请前往 https://www.quartz-scheduler.net/documentation/quartz-3.x/quick-start.html