.netvb.netbrowserhttplistener

Connecting through Https sites


I'm creating an API for a particular web site. This API will be in the form of a referable DLL. I've tried the normal HttpListener but it errors when getting any data that is through https (basically anything specific about the currently logged in user). I also can't log in using it. My next attempt involved a hidden web browser but that is just as hacked up as it sounds (and very glitchy seeing as the WebBrowser likes to interrupt processes that are currently running with its events). I need a system that works and is fast and efficient (I know, I know, kind of a oxymoron). Does anybody know how to do this?

I'm using VB.net so any .net code examples are acceptable.


Solution

  • Here's some C#.net code that I wrote to retrieve XML over HTTPS. The "url" parameter passed in is the url to the https resource you're getting data from. The really tricky part is getting it to pass the cookies to the HTTPS server.

        private XmlDocument getXmlDataFromWebServer(string url)
        {
            System.Net.HttpWebRequest rq = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url);
            System.Net.CookieContainer container = new System.Net.CookieContainer();
            for (int i = 0; i < System.Web.HttpContext.Current.Request.Cookies.Count; i++)
            {
                System.Web.HttpCookie httpcookie = System.Web.HttpContext.Current.Request.Cookies[i];
                string name = httpcookie.Name;
                string value = httpcookie.Value;
                string path = httpcookie.Path;
                string domain = "MySecureDomain.com";
                System.Net.Cookie cookie = new System.Net.Cookie(name, value, path, domain);
                container.Add(cookie);
            }
    
            rq.CookieContainer = container;
            rq.Timeout = 10000;
            rq.UserAgent = "My web site Server Side Code";
    
            System.Net.HttpWebResponse rs = (System.Net.HttpWebResponse)rq.GetResponse();
            System.Text.Encoding enc = System.Text.Encoding.GetEncoding(1252);
            System.IO.StreamReader reader = new System.IO.StreamReader(rs.GetResponseStream());
            System.Xml.XmlDocument xml = new System.Xml.XmlDocument();
            xml.Load(rs.GetResponseStream());
            return xml;
        }