0

I am using C# HttpClient to call the GET web service. However, I am encountering this InnerException : The server committed a protocol violation. Section=ResponseHeader Detail=CR must be followed by LF in.

I tried using WebClient as well, but it had the same issue. The Solution to set useUnsafeHeaderParsing is not an option for me.

I used Fiddler to debug, it shows 200 status code with the returned data. My take on this is the issue is not with the request but with the GetAsync method.

I not am able to make any further progress in solving it. Any help would be highly appreciated.

using (HttpClient client = new HttpClient())
{
       var builder = new UriBuilder("http://url/abc/xyz?..");
       builder.Port = -1;
       var query = HttpUtility.ParseQueryString(builder.Query);
       query["param_1_key"] = "param_1";
       query["param_2_key"] = "param_2";
       query["param3_key"] = "param_3_value";
       builder.Query = query.ToString();
       string url = builder.ToString();

       client.BaseAddress = new Uri(url);
       client.DefaultRequestHeaders.Clear();
       client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));


       var byteArray = Encoding.ASCII.GetBytes("username" + ":" + "password");
       var str = Convert.ToBase64String(byteArray);
       client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray));

       response = client.GetAsync(builder.Uri.AbsoluteUri).Result;
}
Ajinkya Kale
  • 127
  • 1
  • 12
  • So the server you’re calling is returning invalid data, but the provided mitigation is “not an option for you”. What kind of solution are you expecting to find? – stuartd May 19 '18 at 22:53
  • Fiddler and postman get the exact same data, i dont see anything wrong with data unless i am missing any hidden chars. the stacktrace points to Task class. Is there anyway to look at the returned data before task class does the processing? – Ajinkya Kale May 19 '18 at 23:35
  • Are you avoiding "useUnsafeHeaderParsing" because it affects all web requests, not just the individual one you're making? If you want to get really hacky, you should be able to make an HttpGet request using TcpClient. It won't be fun, but you'll get more granular control over how it handles such things. – Doug Johnson May 19 '18 at 23:41
  • Yes thats correct – Ajinkya Kale May 19 '18 at 23:42
  • Possible duplicate of https://stackoverflow.com/questions/8424144/how-to-set-useunsafeheaderparsing-in-code – Todd Menier May 21 '18 at 21:36

0 Answers0