Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Any difference between Console App and Windows Service for HttpWebRequest..? [Solved]

Any difference between Console App and Windows Service for HttpWebRequest..? [Solved]

Scheduled Pinned Locked Moved C#
csharpjsonhelpquestion
7 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    Paladin2000
    wrote on last edited by
    #1

    I am using an HTTP-based API to a 3rd-party service. Its usage involves two steps: POSTing a login command (which saves the session in a cookie), then issuing another command. I have tested this successfully in a console app, but when I try to use it in a (production) Windows service I am getting a 407 Proxy error back. Is there some difference between them regarding HttpWebRequests? I don't believe so but I'm scratching my head here.

    private WebResponse webPOST(string URLextension, string Parameters)
    {
    HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(BASE_URL + URLextension);
    request.ContentType = "application/x-www-form-urlencoded";
    request.Method = "POST";
    request.Headers.Add("X-Requested-With: C# Interface");
    request.CookieContainer = _cookieJar;
    request.Timeout = 3600000; //1 HOUR TIMEOUT
    if (_proxy != string.Empty)
    {
    request.Proxy = new System.Net.WebProxy(_proxy, false);
    request.Proxy.Credentials = new
    NetworkCredential(_proxyUser.ConvertToUnsecureString(), _proxyPass.ConvertToUnsecureString());
    }
    byte[] buffer = Encoding.GetEncoding("UTF-8").GetBytes(Parameters);
    Stream reqstr = request.GetRequestStream();
    reqstr.Write(buffer, 0, buffer.Length);
    reqstr.Close();
    return request.GetResponse();
    }

    The _cookieJar is a private property used to store the cookie generated by login. Both the login command and the "objective" command are issued via this private method, and the entire thing works with no errors in a console app.

    J D P 3 Replies Last reply
    0
    • P Paladin2000

      I am using an HTTP-based API to a 3rd-party service. Its usage involves two steps: POSTing a login command (which saves the session in a cookie), then issuing another command. I have tested this successfully in a console app, but when I try to use it in a (production) Windows service I am getting a 407 Proxy error back. Is there some difference between them regarding HttpWebRequests? I don't believe so but I'm scratching my head here.

      private WebResponse webPOST(string URLextension, string Parameters)
      {
      HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(BASE_URL + URLextension);
      request.ContentType = "application/x-www-form-urlencoded";
      request.Method = "POST";
      request.Headers.Add("X-Requested-With: C# Interface");
      request.CookieContainer = _cookieJar;
      request.Timeout = 3600000; //1 HOUR TIMEOUT
      if (_proxy != string.Empty)
      {
      request.Proxy = new System.Net.WebProxy(_proxy, false);
      request.Proxy.Credentials = new
      NetworkCredential(_proxyUser.ConvertToUnsecureString(), _proxyPass.ConvertToUnsecureString());
      }
      byte[] buffer = Encoding.GetEncoding("UTF-8").GetBytes(Parameters);
      Stream reqstr = request.GetRequestStream();
      reqstr.Write(buffer, 0, buffer.Length);
      reqstr.Close();
      return request.GetResponse();
      }

      The _cookieJar is a private property used to store the cookie generated by login. Both the login command and the "objective" command are issued via this private method, and the entire thing works with no errors in a console app.

      J Offline
      J Offline
      jschell
      wrote on last edited by
      #2

      The windows service is running under a user. Did you test the console app while logged in as that user?

      P 1 Reply Last reply
      0
      • J jschell

        The windows service is running under a user. Did you test the console app while logged in as that user?

        P Offline
        P Offline
        Paladin2000
        wrote on last edited by
        #3

        A good idea, and I tried running the console app under the same account as the service and it worked, so no change there. It always seems to run as a console app but never under the Windows service. I had previously done the reverse (run the service under the same credentials as I was using for the console app), but it did not work. Thanks...

        1 Reply Last reply
        0
        • P Paladin2000

          I am using an HTTP-based API to a 3rd-party service. Its usage involves two steps: POSTing a login command (which saves the session in a cookie), then issuing another command. I have tested this successfully in a console app, but when I try to use it in a (production) Windows service I am getting a 407 Proxy error back. Is there some difference between them regarding HttpWebRequests? I don't believe so but I'm scratching my head here.

          private WebResponse webPOST(string URLextension, string Parameters)
          {
          HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(BASE_URL + URLextension);
          request.ContentType = "application/x-www-form-urlencoded";
          request.Method = "POST";
          request.Headers.Add("X-Requested-With: C# Interface");
          request.CookieContainer = _cookieJar;
          request.Timeout = 3600000; //1 HOUR TIMEOUT
          if (_proxy != string.Empty)
          {
          request.Proxy = new System.Net.WebProxy(_proxy, false);
          request.Proxy.Credentials = new
          NetworkCredential(_proxyUser.ConvertToUnsecureString(), _proxyPass.ConvertToUnsecureString());
          }
          byte[] buffer = Encoding.GetEncoding("UTF-8").GetBytes(Parameters);
          Stream reqstr = request.GetRequestStream();
          reqstr.Write(buffer, 0, buffer.Length);
          reqstr.Close();
          return request.GetResponse();
          }

          The _cookieJar is a private property used to store the cookie generated by login. Both the login command and the "objective" command are issued via this private method, and the entire thing works with no errors in a console app.

          D Offline
          D Offline
          Dave Kreskowiak
          wrote on last edited by
          #4

          The window service is running, by default, under the Local System account, which probably does not have access to your proxy server. The 407 error is Authentication Required, which is why your code works when running from a Console app, which is run by you and runs AS you and it doesn't work as a service.

          A guide to posting questions on CodeProject[^]
          Dave Kreskowiak

          P 1 Reply Last reply
          0
          • D Dave Kreskowiak

            The window service is running, by default, under the Local System account, which probably does not have access to your proxy server. The 407 error is Authentication Required, which is why your code works when running from a Console app, which is run by you and runs AS you and it doesn't work as a service.

            A guide to posting questions on CodeProject[^]
            Dave Kreskowiak

            P Offline
            P Offline
            Paladin2000
            wrote on last edited by
            #5

            I understand what you are saying, but that is not the case here. The sevice is running under the same credentials used for proxy authentication; I changed it after I first installed the service by running services.msc and changing it on the "Log On" tab on the service's properties. I have run the console app under that account though, and it worked (unlike the service). What you're saying is valid, but it was already covered... But I'm still scratching my head on this. :confused:

            1 Reply Last reply
            0
            • P Paladin2000

              I am using an HTTP-based API to a 3rd-party service. Its usage involves two steps: POSTing a login command (which saves the session in a cookie), then issuing another command. I have tested this successfully in a console app, but when I try to use it in a (production) Windows service I am getting a 407 Proxy error back. Is there some difference between them regarding HttpWebRequests? I don't believe so but I'm scratching my head here.

              private WebResponse webPOST(string URLextension, string Parameters)
              {
              HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(BASE_URL + URLextension);
              request.ContentType = "application/x-www-form-urlencoded";
              request.Method = "POST";
              request.Headers.Add("X-Requested-With: C# Interface");
              request.CookieContainer = _cookieJar;
              request.Timeout = 3600000; //1 HOUR TIMEOUT
              if (_proxy != string.Empty)
              {
              request.Proxy = new System.Net.WebProxy(_proxy, false);
              request.Proxy.Credentials = new
              NetworkCredential(_proxyUser.ConvertToUnsecureString(), _proxyPass.ConvertToUnsecureString());
              }
              byte[] buffer = Encoding.GetEncoding("UTF-8").GetBytes(Parameters);
              Stream reqstr = request.GetRequestStream();
              reqstr.Write(buffer, 0, buffer.Length);
              reqstr.Close();
              return request.GetResponse();
              }

              The _cookieJar is a private property used to store the cookie generated by login. Both the login command and the "objective" command are issued via this private method, and the entire thing works with no errors in a console app.

              P Offline
              P Offline
              Paladin2000
              wrote on last edited by
              #6

              I have solved the mystery... The problem was not in the code above, but in what was done with the response stream, which is in XML. Unfortunately, that XML contains a DTD that was causing the problem, when the XmlDocument class tried to retrieve it and failed (because it's not using the proxy credentials properly). And, of course, the "difference" was that in my console app I was just spitting the XML onto the screen, not loading an XmlDocument class. Thanks for the help for those who looked, though... :thumbsup:

              J 1 Reply Last reply
              0
              • P Paladin2000

                I have solved the mystery... The problem was not in the code above, but in what was done with the response stream, which is in XML. Unfortunately, that XML contains a DTD that was causing the problem, when the XmlDocument class tried to retrieve it and failed (because it's not using the proxy credentials properly). And, of course, the "difference" was that in my console app I was just spitting the XML onto the screen, not loading an XmlDocument class. Thanks for the help for those who looked, though... :thumbsup:

                J Offline
                J Offline
                jschell
                wrote on last edited by
                #7

                Timothy CIAN wrote:

                And, of course, the "difference" was that in my console app I was just spitting the XML onto the screen, not loading an XmlDocument class.

                Ahh... When I deliver a windows service I deliver it with a corresponding console app. The service code and the console code do nothing except call to the real functionality of the system.

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups