Click here to Skip to main content
15,792,608 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:

I am trying to call a web service on my client's web server to retrieve XML data. I use HttpWebRequest to call the web service. In the response, I get data in text/html type as my request fails and the client web server is redirecting me to their general web page. I'm guessing that my request is ending up with 4XX error from the web server behaviour (redirect). The thing is that I can actually get the correct response if I just cut and paste the URI that I'm using to create the HttpWebRequest object directly to the browswer. It is failing when I call it from my ASP.Net code. I'm not sure what I'm missing as it is a very simple code. By the way, I'm not able to use the way of adding a reference of a web service to the project and using the web service as an object because of our client side reason, so I have to use HttpWebRequest.

Here is my code.

objRequest = HttpWebRequest.Create(strURI)
objRequest.Method = "GET"
objResponse = objRequest.GetResponse()
objReader = New StreamReader(objResponse.GetResponseStream())
strXML = objReader.ReadToEnd()
objXML = New XmlDocument()

Thanks in advance.
fjdiewornncalwe 12-Feb-13 13:30pm    
It sounds like the web service uses authentication. When you send it from your browser you are likely passing your credentials along. Your web server runs under a service account and as such, it will make the request without credentials unless you specify them.
Sandeep Mewara 12-Feb-13 14:23pm    
I agree with your thought. +5.
Meg Takahashi 13-Feb-13 16:17pm    
Hi Marcus,
Thanks for your comment. I tried the follwoing and it didn't fix.

objRequest.PreAuthenticate = True
objRequest.Credentials = CredentialCache.DefaultNetworkCredentials

Am I doing wrong?
Guirec 12-Feb-13 22:47pm    
Could also be that you are behind a proxy. No problem within browser but you have to set the proxy details inside the code as well (or in the System.Net part of your web.config). Check here :
Meg Takahashi 13-Feb-13 16:20pm    
Hi Cuirec,
Thanks for your comment. I will definitely try your suggestion!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900