.net call a webservice with credentials

jackjohnson44
jackjohnson44 used Ask the Experts™
on
I have some code that calls a webservice on a different server.  I think the web service is java, but I don't know.  When the webservice has no authentication, it works.  When it does have authentication, I get a 501 error.

How can I call the web service with credentials?


SearchService svc = new global::Search.SearchService();
svc.Url = svcAddress;
SearchRequest req = new SearchRequest();
SearchResponse resp = svc.search(req); //this fails when pointing at a url that needs auth

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Miguel OzSenior Software Engineer
Top Expert 2009
Commented:
What kind of authentication does the web service use? You need to find if it is java or .net.
If it is windows authentication, just use UseDefaultCredentials  property:
SearchService svc = new global::Search.SearchService();
svc.Url = svcAddress;
svc.UseDefaultCredentials  = true;

Check:
http://quickstart.developerfusion.co.uk/QuickStart/webservices/doc/UseDefaultCredentials.aspx
http://support.microsoft.com/kb/813834

Author

Commented:
I am not trying to pass default credentials, I am trying to pass credentials manually.  I believe it is java, but I can't figure it out, and there is no one to ask.

SearchService svc = new global::Search.SearchService();
svc.Credentials = new System.Net.NetworkCredential("user", "pass");
//If I DO NOT include the above line where I specify the credentials, I get this error
//The request failed with HTTP status 401: Unauthorized.

//If I DO include, I get this error
//The request failed with HTTP status 505: HTTP Version Not Supported.

svc.Url = svcAddress;
SearchRequest req = new SearchRequest();
SearchResponse resp = svc.search(req); //this fails when pointing at a url that needs auth

Author

Commented:
Also, if I go directly to the web service url in firefox, I get a login popup box.
I solved it!

Add this line and it works:
svc.PreAuthenticate = true;

Author

Commented:
It was svc.PreAuthenticate = true;

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial