Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# https request always tunnel to server but Java only once

I'm converting JAVA program to C# that send https request to server. Here is my program that log in to server and then log-out from server.

  var login_httpWebRequest = (HttpWebRequest)WebRequest.Create(m_base_url + "session/login");
  login_httpWebRequest.ContentType = "application/json; charset=UTF-8";

  var logout_httpWebRequest = (HttpWebRequest)WebRequest.Create(m_base_url + "session/logout");
  logout_httpWebRequest.ContentType = "application/json; charset=UTF-8";

  CookieContainer cookieJar = new CookieContainer();
  login_httpWebRequest.CookieContainer = cookieJar;


  using (StreamWriter streamWriter = new StreamWriter(login_httpWebRequest.GetRequestStream()))
  {
      streamWriter.Write("{username:xxxxxx,password:yyyyyyy}");
  }
  var httpResponse = (HttpWebResponse)login_httpWebRequest.GetResponse();
  var login_cookies = httpResponse.Cookies;

  logout_httpWebRequest.CookieContainer = new CookieContainer();
  httpWebRequest.CookieContainer.Add(login_cookies);
  using (StreamWriter streamWriter = new StreamWriter(logout_httpWebRequest.GetRequestStream()))
  {
      streamWriter.Write("{}");
  }

  var httpResponse = (HttpWebResponse)login_httpWebRequest.GetResponse();

But my C# program takes more time than the original JAVA program and then I checked the request using Fiddler tool. So I found for every request C# Tunnel to server. but JAVA program only once.

Bellow image

  • Green color shows requests (tunnel->login->logout) sent to server by JAVA program.
  • Red color shows C# program requests(tunnel->login->tunnel->logout) sent to server by C# program.

enter image description here

Anyone knows how to avoid tunneling for each request by C# Http client.

like image 383
Nayana Adassuriya Avatar asked Oct 02 '22 08:10

Nayana Adassuriya


1 Answers

Have you tried using HttpClient instead of WebRequest? I wrote this up with no way to test it so hopefully it's close....

    using (HttpClientHandler handler = new HttpClientHandler())
    {
        CookieContainer cookies = new CookieContainer();
        handler.CookieContainer = cookies;

        using (HttpClient httpClient = new HttpClient(handler))
        {
            httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json; charset=UTF-8"));

            HttpRequestMessage requestMessage = new HttpRequestMessage(HttpMethod.Get, m_base_url + "session/login");
            requestMessage.Content = new StringContent("{username:xxxxxx,password:yyyyyyy}"); ;

            HttpResponseMessage loginResponse = await httpClient.SendAsync(requestMessage);

            // The HttpClient should already have the cookies from the login so
            // no need to transfer
            requestMessage = new HttpRequestMessage(HttpMethod.Get, m_base_url + "session/logout");
            requestMessage.Content = new StringContent("{}");

            HttpResponseMessage logoutResponse = await httpClient.SendAsync(requestMessage);
        }
    }
like image 111
BrianM Avatar answered Oct 18 '22 22:10

BrianM