简体   繁体   中英

Non-firing quartz-scheduler job

I have a project containing data scraping process from a password protected website. The data scraping process is set to a quartz-scheduler job, which is triggered once an hour. When I deploy the project to web, the process works fine but after 1 hour, the next trigger time, it cannot be executed.

I am sure quartz-scheduler usage is true, because a have another job done with no problem. I think problem is about socket issues which I am not good at.

Here is the code:

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);
            }
        }
    }
}

I get help to create this code, so I dont know some part of this code very well. I suspect the socket part. I will be very happy if someone can help.

Quartz has a logger included. You just need to plug to it to retrieve any exception that might occur in your code

If you're using .Net Core, head to https://www.quartz-scheduler.net/documentation/quartz-3.x/quick-start.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM