0

If I put the URL at the browser, my server responds properly (a XML). Although, if this same URL pass through the WebClient.DownloadingString() method, something in the URL changes, and my server responds properly, but with an access denied message (XML, too), as if something had changed.

"Error message"

<?xml version="1.0" encoding="ISO-8859-1"?><said:service xmlns:said="http:xxx"><said:codigo_erro>8</said:codigo_erro><said:mensagem_erro>Unable</said:mensagem_erro></said:service>

The URL used on request is like this one:

http://...<parameter1>S<%2Fparameter1>%0D%0A++<parameter2>S<%2Fparameter2>%0D%0A++<parameter3>S<%2Fparameter3>%0D%0A<%2Fqueryservice>%0D%0A%09%09

I have already tried change de Encode to UT8, ISO, etc. No one of them worked.

Konrad Viltersten
  • 36,151
  • 76
  • 250
  • 438

1 Answers1

1

You have to be sure that you're sending all the necessary data, cookies and request headers that the server is expecting.

I advise you to install Fiddler Web Debugger and monitor successful requests from web browser, after that try to recreate such requests in your application.

Maybe server is redirecting you to some error page because WebClient is not handling cookies. You can create your own version of WebClient and add cookie support. Create a class that inhertis from WebClient and override GetWebRequest method, there you have to add CookieContainer. Following is a simple implementation of WebClient that handles cookies:

public class MyWebClient : WebClient
{
    public CookieContainer CookieContainer { get; private set; }

    public MyWebClient()
    {
        this.CookieContainer = new CookieContainer();
    }

    protected override WebRequest GetWebRequest(Uri address)
    {
        WebRequest request = base.GetWebRequest(address);
        if (request is HttpWebRequest)
        {
            (request as HttpWebRequest).CookieContainer = this.CookieContainer;
            (request as HttpWebRequest).AllowAutoRedirect = true;
        }

        return request;
    }
}
Ivan Golović
  • 8,732
  • 3
  • 25
  • 31