Register for your free account! | Forgot your password?

Go Back   elitepvpers > Coders Den > General Coding
You last visited: Today at 15:04

  • Please register to post and access all features, it's quick, easy and FREE!

Advertisement



[C#] HtmlAgilityPack doesn't work with Windows Forms but works in Console Application

Discussion on [C#] HtmlAgilityPack doesn't work with Windows Forms but works in Console Application within the General Coding forum part of the Coders Den category.

Reply
 
Old   #1
 
Saisama's Avatar
 
elite*gold: 0
Join Date: Apr 2019
Posts: 52
Received Thanks: 7
[C#] HtmlAgilityPack doesn't work with Windows Forms but works in Console Application

I've got a HTML document with HtmlWebRequest. Then I've tried to parse it and getting a value out of it using HtmlAgilityPack. I've found out that if I do this in a console application there is not any issue and I can get the value. But if I use a Windows Forms application with this same code to get this value and assign it to another variable, it doesn't work and I get a NullReferenceException. And what I've found out is that my Windows Forms application works while Fiddler is running. And this console application works without Fiddler.

What I've tried so far:
  • Using a proxy
  • Compared GET request's what my application sends to Internet Explorer's request. They looked similar.
  • Using RegEx instead of HtmlAgilityPack

Console Application:

Code:
using HtmlAgilityPack;
using System;
using System.IO;
using System.Net;
using System.Threading;

namespace HttpTest
{
    class Program
    {
        static void Main(string[] args)
        {
            Thread worker;
            var token = "";
            worker = new Thread(() =>
            {
                token = getToken();
                Console.WriteLine("Token: " + token);
            });
            worker.Start();
        }

        public static string GetRequest(string url, string host, bool keepAlive)
        {
            CookieContainer cookie = new CookieContainer();
            string html = string.Empty;
            var req = (HttpWebRequest)WebRequest.Create(url);
            req.Method = "GET";
            req.Host = host;
            req.KeepAlive = keepAlive;
            req.UserAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko";
            req.Timeout = 15000;
            req.Accept = "text/html, application/xhtml+xml, image/jxr, */*";
            req.CookieContainer = cookie;

            var resp = (HttpWebResponse)req.GetResponse();

            var stream = new StreamReader(resp.GetResponseStream());
            html = stream.ReadToEnd();

            stream.Close();
            resp.Close();
            return html;
        }

        public static string getToken()
        {
            var content = GetRequest("url", "host", true);
            string signupFormId;

            var doc = new HtmlAgilityPack.HtmlDocument();
            doc.LoadHtml(content);
            HtmlNode signupFormIdElement = doc.DocumentNode.SelectSingleNode("//input[@name='login']");
            signupFormId = signupFormIdElement.GetAttributeValue("value", "");
            return signupFormId;
        }
    }
}
Error which I get in my Forms app:
Quote:
`System.NullReferenceException: 'Object reference not set to an instance of an object.'

signupFormIdElement was null.`
My WindowsForms application starts the main thread with a button click.

When I debugged the application I've find out that

var resp = (HttpWebResponse)req.GetResponse();

this expression returns null.

Edit:
After hours of debugging I finally found there is a socket error, and I forgot that 2 days ago I changed machine.config file to configure Fiddler as .NET proxy.
Saisama is offline  
Reply


Similar Threads Similar Threads
[C#] Console-Application crasht bei SetConsoleCtrlHandler und Console.ReadLine()
05/11/2015 - .NET Languages - 4 Replies
Servus! Ich habe derzeit ein komisches Problem: Ich schreibe eine Konsolenanwendung und möchte, bevor die Konsole geschlossen wird, den Speicher aufräumen (manuelles GC). private delegate bool ConsoleEventDelegate(int eventType); private static extern bool SetConsoleCtrlHandler(ConsoleEventDelegate callback, bool add);
C# Using HtmlAgilityPack
07/18/2012 - .NET Languages - 1 Replies
Hi guys i am facing an annoing problem please i wanna to have your help with it i have the following html <div class="sideInfo"> <a class="getLink" href="info"> <span class="wrap">NEW</span> </a> <img class="nation" src="img/xyz.png" alt="NEW">
When I Press Start Game, CR Closed but Game doesn't work
07/09/2012 - Cabal Online - 0 Replies
I run CR.Login with ID-Pasword.And click start game. Cabalmain.exe appear in ctrl+alt+delete(task manager) but game doesn't work. Thank for answers :) UP UP
Cert server start on 192.168.x.x but doesn't work with real IP.
09/30/2011 - SRO Private Server - 4 Replies
So, i can run the server on 192.168.x.x (local ip) but i can't run it on my real ip. I get an error related to socket ip or smthing like that, then it says certification server has stopped working. here is a screenshot of the error: http://i53.tinypic.com/9h2nmv.png
softmod works, but bot doesn't start.
05/15/2007 - Silkroad Online - 5 Replies
yay anumis for putting new softmod that works!!!! just one minor problem... i try to start bot by lower left hand corner on my taskbar, but i press start game, and it doesn't do anyhing..



All times are GMT +2. The time now is 15:04.


Powered by vBulletin®
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
SEO by vBSEO ©2011, Crawlability, Inc.
This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Support | Contact Us | FAQ | Advertising | Privacy Policy | Terms of Service | Abuse
Copyright ©2024 elitepvpers All Rights Reserved.