How to Specify a Web Proxy for a Service Reference? - c#

I found a simple web service online at http://chennaiemergency.co.in/sree/s2.php?wsdl which i am able to invoke through SOAP UI using 2 float values (1,1) and easily get the response within 1-2 seconds.
Now, in a new visual studio 2010 console application project I do "add service reference" and provide the WSDL. Then in the resulting client i do this :
ServiceReference1.ChnEmergencyPortTypeClient client = new ChnEmergencyPortTypeClient();
string hospital = client.hospital(1, 1);
I get timeout exception after 1 minute.
I have disabled firewall for sure.
I am using Windows7x64
I am using internet via proxy server.
I tried same thing via adding web reference, but i got same timeout error.
Now in the web reference implementattio I have done these few modifications :
WebProxy webProxy = new WebProxy("<my proxy server name>", <port>);
ChnEmergency client = new ChnEmergency();
client.Timeout = 200000;
client.Proxy = webProxy;
string hospital = client.hospital(1, 1);
But i still get timeout. Any suggestions where i am missing ?
I did a quick test with a direct (via phone) internel connection which does not involve proxy servers. And i was able to access successfully. This indicates that there is fault in the way i am providing the webproxy. The IE internet setting suggests that my proxy settings are :
Address : a.b.c.com
Port : 80
So i am bulding a webproxy like this
WebProxy webProxy = new WebProxy("a.b.c.com", 80);
Now i dont know if there is some "secure http" concept involved somewhere, nor do i know how to figure out. But a quick try on browser with http://a.b.c.com and https://a.b.c.com yielded different results. In the "http" case i got that invalid url. In "https" case error is "Google Chrome's connection attempt to a.b.c.com was rejected. The website may be down, or your network may not be properly configured"
If i use webproxy with https, it says "service point manager is not configure for https"
I used fiddler to see the activities and I see that the request does show up in fiddler. But no response comes. Does this necessarily mean that the request is going through ? Or could the request get blocked at a lower level (ie. after it goes through fiddler).

I tried quickly making a small web project and it seems to work fine, but the way you have added the webservice it returns xml like this:
<?xml version="1.0" encoding="ISO-8859-1"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Body>
<ns1:hospitalResponse xmlns:ns1="urn:ChnEmergency">
<return xsi:type="xsd:string">12.944672~80.134578~Chrompet GH~Government~GST Road, Chrompet~Chrompet~600036</return>
</ns1:hospitalResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
If your Proxy needs a password, you could try:
var proxy = new WebProxy("proxy.foo.com", 8080);
proxy.Credentials = new NetworkCredential("user", "pass");
WebRequest.DefaultWebProxy = proxy;

Well it turns out that the proxy i was using, proxyA, was inturn redirecting through another proxy, proxyB. Using the proxyB directly resolved the issue. I was informed about this by someone else, didnt figure out myself. I used the proxy object like WebProxy webProxy = new WebProxy("proxyB.com", 80). So issue is not solved, but resolved for now.

Related

WCF Unsecured response in .NET 4.5

I'm working on client application to utilize SOAP web service. Added SOAP web services as Service reference. It connects to IBM server and server requires WS-Security basic authentification.
Called with default settings and got an error(no authentication header)
Modified code to look like so:
var service = new RealTimeOnlineClient();
service.ClientCredentials.UserName.UserName = "xxxxx";
service.ClientCredentials.UserName.Password = "yyyyy";
Now when I look at response in Fiddler - works properly (I get expected envelope from server), I get proper envelope back.
However, I get exception from WCF:
Security processor was unable to find a security header in the message. This might be because the message is an unsecured fault or because there is a binding mismatch between the communicating parties. This can occur if the service is configured for security and the client is not using security.
Quick search and bunch of answers here on SO points me to HotFix from Microsoft where they added new property EnableUnsecuredResponse. Problem is - I can't figure out WHERE to apply this property in my code OR in config. Adding to security tag in web.config doesn't work (errors out can't find property).
I understand hotfix came out for .NET 3.5 and most questions from 2009-2010. It should be in 4.5 already, correct? How do I apply this property to my code?
I had to add following code to alter value of "EnableUnsecureResponse"
var service = new RealTimeOnlineClient();
service.ClientCredentials.UserName.UserName = "xxxxx";
service.ClientCredentials.UserName.Password = "yyyyy";
var elements = service.Endpoint.Binding.CreateBindingElements();
elements.Find<SecurityBindingElement>().EnableUnsecuredResponse = true;
service.Endpoint.Binding = new CustomBinding(elements);

How to use proxy like browser OR CredentialCache.DefaultCredentials different between XP and 7

I am able to fix a problem with a client where they cannot authenticate through a proxy doing the following:
var proxy = WebRequest.GetSystemWebProxy();
proxy.Credentials = CredentialCache.DefaultNetworkCredentials;
service.Proxy = proxy;
This works fine for Windows XP, however on Windows 7 I get a 407 (proxy not authenticated exception). Does anybody know what the difference is, and more importantly, what I need to do to get this to work on both OS?
UPDATE
I am having the users check the following:
In the registry editor, can you go to HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon and let me know what the value is for CachedLogonsCount. f
In the Start box, type in Group Policy and an option to Edit Group Policy should pop up, click on it. Then go to Computer Configuration\Administrative Templates\System\User Profiles\Delete cached copies of roaming profiles and let me know if it is configured, and if so, to what is it set?
UPDATE FOR BOUNTY
So, I added the bounty. I can take a solution from here, or just an alternate means to getting through a proxy on Windows 7...
Another Update
I am not sure if this is useful or not, but we are also doing the following:
service.PreAuthenticate = true;
service.Url = "myurl";
service.Credentials = new NetworkCredential(txt_UserName.Text, txt_Password.Text);
My temporary solution
This is not really a solution, but works for now. I am using the app.config and setting the proxy to be default, with a ByPassList so that the proxy is not even used. This is only doable since the proxy does not have a strong firewall currently. For other clients, I need to get the above to work
This piece of code works for me on XP, Win7 and 2008
var webProxy = new WebProxy(WebRequest.DefaultWebProxy.GetProxy(new Uri({TheURLoftheService})));
webProxy.Credentials = CredentialCache.DefaultCredentials;
webProxy.UseDefaultCredentials = true;
service.Proxy = webProxy;
actually looks like they "fixed" it in Win7 :) Can you confirm that both client and server are specifying http 1.1
Now let's discuss as to why the browser works in this scenario. IE
uses WinINet under the hood rather than WinHTTP. If we look at the
network traces we see that IE sends HTTP/1.1, but the proxy replies
with HTTP/1.0. IE still accepts this behavior, because in the internet
scenario there are countless number of clients and servers which still
use HTTP/1.0.
WinHTTP strictly requires HTTP/1.1 compliance for keeping the
connection alive and HTTP Keep-Alives are not supported in HTTP/1.0
protocol. HTTP Keep-Alive feature was introduced in the HTTP/1.1
protocol as per RFC 2616. The server or the proxy which expects the
keep-alive should also implement the protocol correctly. WinHTTP on
Windows 7, Windows 2008 R2 are strict in terms of security wrto
protocol compliance. The ideal solution is to change the server/proxy
to use the right protocol and be RFC compliant.
http://blogs.msdn.com/b/httpcontext/archive/2012/02/21/changes-in-winhttp-on-windows-7-and-onwards-wrto-http-1-0.aspx
Will this work?
I am using this to set proxy, so far we did not encounter an error on all windows platform
Uri address = new Uri("http://your-webservice-address");
//Get User current network credential
ICredentials credentials = CredentialCache.DefaultCredentials;
NetworkCredential credential = credentials.GetCredential(address, "Basic");
//Get HttpWebRequest
HttpWebRequest request = WebRequest.Create(address) as HttpWebRequest;
//Network Credential should be included on the request to avoid network issues when requesting to the web servic
request.Proxy = WebRequest.DefaultWebProxy;
request.Credentials = new NetworkCredential(credential.UserName, credential.Password, credential.Domain);
It's hard to say based on the code you've given. I'd suspect that it's either your IE settings or your proxy variables.
Check http://social.msdn.microsoft.com/Forums/en/netfxnetcom/thread/61b71194-1758-4c7b-89fe-91be7363db13 it may help.

Certificate validation doesn't use proxy settings for chaintrust

I've been struggling to get a WCF client to work through a web proxy. If I manually specify the proxy as below, I can get the http request to work.
WebProxy proxy = new WebProxy("http://x.x.x.x:3128", false);
proxy.Credentials = new NetworkCredential("user", "pass");
WebRequest.DefaultWebProxy = proxy;
However I have the client service proxy set to use ClientCredentials.ServiceCertificate.Authentication.CertificateValidationMode = System.ServiceModel.Security.X509CertificateValidationMode.ChainTrust;
Inspecting the packets that get sent out, the client successfully connects to the server, but the then tries to validate the certificate that the service provides. This validation attempt fails because the requests for the chain do not have Proxy-Authorization headers (they fail with 407 errors). How can I get these requests to properly use the DefaultWebProxy that's specified?
If I set the validation mode to None, then it all works of course, but I really don't want to have to do that.
this setting only affects message level certificates. for transport level try something like this http://webservices20.blogspot.co.il/2008/12/wcf-gotcha-disabling-ssl-validation.html

why won't WebProxy BypassProxyOnLocal work for me?

I'm trying to get HTTP calls I'm making from C# .NET to a local address (localhost:3000) to use the proxy I set (so I can go through fiddler). Using the below WebProxy approach works if I point the target URL to a non-local address, however I need to point it to a local web-server I have (at localhost:3000), and when I do this the request is not going through the proxy.
I have inlcuded the "proxyObject.BypassProxyOnLocal = false". This should make it work no? Any suggestions re how to force the request to go through the WebProxy for http calls targetting a local address?
WebProxy proxyObject = new WebProxy("http://localhost:8888/", false);
proxyObject.Credentials = new NetworkCredential();
proxyObject.BypassProxyOnLocal = false;
WebRequest.DefaultWebProxy = proxyObject;
var request = (HttpWebRequest)WebRequest.Create(targetUri);
// I also included this line as a double check
request.Proxy = proxyObject;
Subsequent calls do not go through the proxy however, such as when I do:
var res = (HttpWebResponse)req.GetResponse();
thanks
I get around this simply by appending a "dot" to localhost, so instead of accessing "localhost", I try to access "localhost." (notice the dot at the end of the hostname)
Credit where credit is due:
I got this unusual tip from this thread http://www.west-wind.com/weblog/posts/2009/Jan/14/Monitoring-HTTP-Output-with-Fiddler-in-NET-HTTP-Clients-and-WCF-Proxies#596591
Works fine!
See explanation on https://docs.telerik.com/fiddler/observe-traffic/troubleshooting/notraffictolocalhost
Internet Explorer and the .NET Framework are hardcoded not to send requests for Localhost through any proxies, and as a proxy, Fiddler will not receive such traffic.
The simplest workaround is to use your machine name as the hostname instead of Localhost or 127.0.0.1. So, for instance, rather than hitting http://localhost:8081/mytestpage.aspx, instead visit http://machinename:8081/mytestpage.aspx.

WebProxy and Authentication on Vista

I am talking to a webservice via a webrequest, I am behind a proxy that requires authentication.
What I would like to do is piggyback off the IE / Control Panel settings but I am having some difficulty...
if I do this, all is fine...
WebProxy proxy = new WebProxy(#"http://my.secret.address:8080");
proxy.Credentials = CredentialCache.DefaultNetworkCredentials;
WebRequest.DefaultWebProxy = proxy;
What I really want to do is simply this...
WebRequest.DefaultWebProxy = WebRequest.GetSystemWebProxy(); // Subsequent webrequest call Fails with a "Unable to connect to remote server" error message.
I really do not want to have to specify the proxy address, as it is not the same for all users. In fact some won't even be behind a proxy. I just want to use the IE /Control Panel settings. Oh I am using Vista in case that makes a difference, and also the proxy settings in th econtrol panel / IE are using an auto config file (proxy.pac file)
Edit: So succinctly. How do I use the IE / Control Panel proxy settings. Including when using an Auto configuration file ?
Further Edit:
Ok, I think I have narrowed down the problem to the Auto Config thing. If I have the proxy address explicitly set in the dialog I can use the .GetSystemWebProxy() settings...but (like in my case) if I am using an Auto Config pac file, I have this issue.
alt text http://img40.imageshack.us/img40/5635/57955210.jpg
In .NET 1.0, you could use:
WebRequest.DefaultWebProxy = WebProxy.GetDefaultProxy();
In 2.0, DefaultWebProxy is supposed to contain the IE proxy settings by default, so this method is obsolete.
http://www.west-wind.com/WebLog/posts/2542.aspx has more information.
UPDATE: Apperently the .NET 2.0 method is now;
WebRequest.DefaultProxy = WebRequest.GetSystemWebProxy();
http://msdn.microsoft.com/en-us/library/system.net.webrequest.getsystemwebproxy.aspx
sigh, well after more investigation I fixed this problem just to get a different one....
The fix is to create the WebProxy with the .pac Uri
WebProxy proxy = new WebProxy(#"http://blahblah/proxy.pac);
Easy peasy...
So now I am getting through the proxy, but the Proxy server is messing with my request and the web service is barffing. (Note it doesn't do it if I am specific about the proxy address....sigh)

Categories