2

I'm creating an API for a particular website. This API will be in the form of a referable DLL. I've tried the normal HttpListner 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 webbrowser 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. Thanks if you can!

Vivian River
  • 31,198
  • 62
  • 198
  • 313
Freesnöw
  • 30,619
  • 30
  • 89
  • 138

2 Answers2

1

So if I want to use your API I download your DLL and talk to it and it in turn then talks to your centralized system over HTTPS? Do I have that part correct?

My first attempt at this would be to just build a web service and have my DLL talk to that. (Actually, I wouldn't bother with the DLL if I could but I understand you probably have your reasons.)

If I couldn't do that I would just use the built in WebClient or the HttpWebRequest classes. You'll have to handle cookies manually but its not that hard.

When dealing with System.Net you sometimes have to set some global properties which can be found in the ServicePointManager class. Specifically Expect100Continue, ServerCertificateValidationCallback and UseNagleAlgorithm come to mind.

Lastly, I'd recommend turning on tracing for System.Net.

If this doesn't help you you'll need to give us some more information about what "isn't working" such as any error messages that you might be receiving.

Community
  • 1
  • 1
Chris Haas
  • 53,986
  • 12
  • 141
  • 274
1

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;
    }
Vivian River
  • 31,198
  • 62
  • 198
  • 313