Any difference between Console App and Windows Service for HttpWebRequest..? [Solved]
-
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. -
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. -
The windows service is running under a user. Did you test the console app while logged in as that user?
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...
-
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.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 -
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 KreskowiakI 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: -
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.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:
-
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:
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.