0

I have a requirement where, i need to send a body on a GET request, the .netFramework 4.7.2, throw an Protocol exception, but as far i read, is possible to send it using a WinHttpHandler, but I'm getting a

Error 12175 calling WINHTTP_CALLBACK_STATUS_REQUEST_ERROR, 'A security error occurred

im setting the handler protocols and certification callback, but im on a dead end.

WinHttpHandler handler = new WinHttpHandler();
handler.ServerCertificateValidationCallback = (senderX, certificate, chain, sslPolicyErrors) => { return true; };
handler.SslProtocols = SslProtocols.Tls12 | SslProtocols.Tls11 | SslProtocols.Tls;

HttpClient client = new HttpClient(handler);
var requestSELF = new HttpRequestMessage(HttpMethod.Get, "https://ABC/XYX");
requestSELF.Headers.Add("HEADER1", "ABC");
requestSELF.Headers.Add("HEADER2", "ABC");
                
var content = new StringContent(JsonUtils.ConvertToJson(body), null, "application/json");
requestSELF.Content = content;
var responseHARD = client.SendAsync(requestSELF).Result;

I'm unable to change the fact that the client set a body on a get verb, is what i what got to work with, but i cant find a happy solution, my alternative is to make a JS file and get result from it, but it broke the project standards.

Peter Csala
  • 17,736
  • 16
  • 35
  • 75

2 Answers2

0

Using body in GET method is not recommended but still you can achieve this. Try using RestSharp. Use the following:

var client = new RestClient("https://localhost:7181");
    var request = new RestRequest("/ContactDirectory/AddContactInfo", Method.Get);
    string jsonToSend = JsonConvert.SerializeObject(model);
    request.AddParameter("application/json", jsonToSend, ParameterType.RequestBody);
    request.RequestFormat = DataFormat.Json;

    try
    {
        RestResponse restResponse =  client.Execute(request);

        var strContent = restResponse.Content;

        
    }
    catch (Exception error)
    {
        // Log
    }

For more details you can also have a look at this detailed thread.

How to use HttpClient to send content in body of GET request?

Safee
  • 58
  • 4
0

I found a solution based on @safee link, the trick this time, was to modify the HTTP Verb to ignore all the protocols and using WebRequest.

After the

WebRequest.Create

I added this.

   var type = request.GetType();
            var currentMethod = type.GetProperty("CurrentMethod", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(request);
            var methodType = currentMethod.GetType();
            methodType.GetField("ContentBodyNotAllowed", BindingFlags.NonPublic | BindingFlags.Instance).SetValue(currentMethod, false);

add the body on a stream Writer and add the SecurityProtocols for the SSL.

   using (var streamWriter = new StreamWriter(request.GetRequestStream()))
            {
                streamWriter.Write(JsonConvert.SerializeObject(body));
            }

           
            var response = request.GetResponse();
            var responseStream = responseSR.GetResponseStream();


           
            if (responseStream != null)
            {
                var myStreamReader = new StreamReader(responseStream, Encoding.Default);
                var resultEntity = myStreamReader.ReadToEnd();
                myStreamReader.ReadToEnd();
            }

         
            responseStream.Close();
            responseSR.Close();