1

I've been tasked with building a service which pulls information from a 3rd party API into our internal datawarehouse. They have a get request to pull the data I want where you specify the parameters you want via query strings. E.g.

http://www.api.com?parameter=firstname&parameter=surname

In my code the length of the URL is over 3600 characters long as the requirement is for 116 parameters.

My web request is generated using this code:

    private HttpWebRequest GetWebRequest(string url, string type, int timeout)
    {
        var httpWebRequest = (HttpWebRequest) WebRequest.Create(_baseUrl + url);
        httpWebRequest.Method = type;
        httpWebRequest.Timeout = timeout;            
        httpWebRequest.ContentType = "application/json";
        httpWebRequest.Headers.Add("Authorization", "Bearer " + _token.access_token);
        httpWebRequest.ContentLength = 0;

        return httpWebRequest;
    }

When I run the code I am getting back a web exception with the message "Unable to connect to the remote server" with an internal exception message of "No connection could be made because the target machine actively refused it IP Address"

I have not included the entire URL in this post as I have found that if I copy and paste the url into Postman and run the request I get the response I expect so I know that the URL is formatted correctly. I have also discovered that if I cut down the length of the url to around 2100 characters the request then works.

I have been searching but have not found any definitive documentation to suggest that there is a limit to the length of the URL, but I can not explain why the whole url works in Postman but not in a c# web request and that if I cut the length of the URL it then works in the web request!

If anyone has any ideas about this I'd be greatfull.

kcis8rm
  • 107
  • 5
  • 12

1 Answers1

0

An old post suggests that depending on the server and client the maximum request length is somewhere between 2 - 4 and 8 KB, which is consistent with your observations.

Postman is 'another' client, so it is well possible that it works there while it doesn't in your program. Bottom-line is: you should not GET such long requests. You should POST them instead. If you have control over the service, you could change it so it supports POST too, if not already (documented or not).

Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325