title: Getting a response from the browser (HTTPS, C#)
body: <p>It is necessary to receive data from the browser, I mean the authorization code. I use the <code>OAuth2</code> from <code>Yahoo</code> service. This code is displayed in the address bar of the browser after successful authorization, than redirect to <code>redirect_uri=...</code>. <code>Process</code> is used to start the browser, after that I try to listen on port <code>XXXX</code> with <code>HttpListener</code>, however even if I get some response, it is a meaningless set of characters . (<code>UTF-8</code>, etc. tried, the result is the same)</p>
  <p>How can I get the authorization code from the browser after successful authorization?</p>
  <h1>Code with <code>HttpListener</code></h1>
  <pre><code>static void SniffPort()
  {
      byte[] input = new byte[] { 1 };
      Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.IP);
      socket.Bind(new IPEndPoint(IPAddress.Broadcast, 5000));
      socket.IOControl(IOControlCode.ReceiveAll, input, null);
  
      byte[] buffer = new byte[4096];
  
      Task.Factory.StartNew(() =&gt;
      {
          while (true)
          {
              int len = socket.Receive(buffer);
              if (len &lt;= 40) continue;
              string bin = Encoding.Default.GetString(buffer, 0, len);
              Console.WriteLine(bin);
          }
      });
  }
  
  static HttpListener _httpListener = new HttpListener();
  
  static void Main(string[] args)
  {
      Console.WriteLine(&quot;Starting server...&quot;);
      _httpListener.Prefixes.Add(&quot;https://localhost:5000/&quot;);
      _httpListener.Start();
      Console.WriteLine(&quot;Server started.&quot;);
      SniffPort();
  
      ProcessStartInfo startInfo = new ProcessStartInfo(@&quot;C:\Users\User\Desktop\chrome-win\chrome.exe&quot;);
      startInfo.WindowStyle = ProcessWindowStyle.Normal;
      Process p = new Process();
      startInfo.Arguments = &quot;--app=https://api.login.yahoo.com/oauth2/request_auth?client_id=CLIENT_ID&amp;redirect_uri=https://localhost:5000/&amp;response_type=code&amp;language=en-us&quot;;
      p.StartInfo = startInfo;
      p.Start();
      p.WaitForExit();
  }
  </code></pre>
  <p>Also I had idea with the <code>FiddlerCore</code>, but after using it, you have to change the settings related to the proxy in <code>inetcpl.cpl</code>. It also doesn't work if the browser remembers the user and logs in immediately after starting the browser, <code>FiddlerCore</code> does not provide this information.</p>
  <h1>Code with <code>FiddlerCore</code></h1>
  <pre><code>public class HttpProxy : IDisposable
  {
      public HttpProxy()
      {
          Fiddler.FiddlerApplication.BeforeRequest += FiddlerApplication_BeforeRequest;
          Fiddler.FiddlerApplication.Startup(5000, true, false);
      }
  
      void FiddlerApplication_BeforeRequest(Fiddler.Session oSession)
      {
          Console.WriteLine(String.Format(&quot;REQ: {0}&quot;, oSession.url));
      }
  
      public void Dispose()
      {
          Fiddler.FiddlerApplication.Shutdown();
      }
  }
  
  static HttpListener _httpListener = new HttpListener();
  
  static void Main(string[] args)
  {
      HttpProxy httpProxy = new HttpProxy();
      
      //Launching the browser as in the code above.
  }
  </code></pre>