[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 966
  • Last Modified:

.net call a webservice with credentials

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

0
jackjohnson44
Asked:
jackjohnson44
  • 4
2 Solutions
 
Miguel OzSoftware EngineerCommented:
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
0
 
jackjohnson44Author 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
0
 
jackjohnson44Author Commented:
Also, if I go directly to the web service url in firefox, I get a login popup box.
0
 
jackjohnson44Author Commented:
I solved it!

Add this line and it works:
svc.PreAuthenticate = true;
0
 
jackjohnson44Author Commented:
It was svc.PreAuthenticate = true;
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now