Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 134085
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T06:32:59+00:00 2026-05-11T06:32:59+00:00

I’m trying to replace this: void ProcessRequest(object listenerContext) { var context = (HttpListenerContext)listenerContext; Uri

  • 0

I’m trying to replace this:

void ProcessRequest(object listenerContext) {     var context = (HttpListenerContext)listenerContext;     Uri URL = new Uri(context.Request.RawUrl);     HttpWebRequest.DefaultWebProxy = null;     HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(URL);     httpWebRequest.Method = context.Request.HttpMethod;     httpWebRequest.Headers.Clear();     if (context.Request.UserAgent != null) httpWebRequest.UserAgent = context.Request.UserAgent;     foreach (string headerKey in context.Request.Headers.AllKeys)     {         try { httpWebRequest.Headers.Set(headerKey, context.Request.Headers[headerKey]); }             catch (Exception) { }     }      using (HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse())     {         Stream responseStream = httpWebResponse.GetResponseStream();         if (httpWebResponse.ContentEncoding.ToLower().Contains('gzip'))             responseStream = new GZipStream(responseStream, CompressionMode.Decompress);         else if (httpWebResponse.ContentEncoding.ToLower().Contains('deflate'))                 responseStream = new DeflateStream(responseStream, CompressionMode.Decompress);          MemoryStream memStream = new MemoryStream();          byte[] respBuffer = new byte[4096];         try         {             int bytesRead = responseStream.Read(respBuffer, 0, respBuffer.Length);             while (bytesRead > 0)             {                 memStream.Write(respBuffer, 0, bytesRead);                 bytesRead = responseStream.Read(respBuffer, 0, respBuffer.Length);             }         }         finally         {             responseStream.Close();         }          byte[] msg = memStream.ToArray();          context.Response.ContentLength64 = msg.Length;         using (Stream strOut = context.Response.OutputStream)         {             strOut.Write(msg, 0, msg.Length);         }     }     catch (Exception ex)     {         // Some error handling     } } 

with sockets. This is what I have so far:

void ProcessRequest(object listenerContext) {     HttpListenerContext context = (HttpListenerContext)listenerContext;     Uri URL = new Uri(context.Request.RawUrl);     string getString = string.Format('GET {0} HTTP/1.1\r\nHost: {1}\r\nAccept-Encoding: gzip\r\n\r\n',                 context.Request.Url.PathAndQuery,                 context.Request.UserHostName);      Socket socket = null;      string[] hostAndPort;     if (context.Request.UserHostName.Contains(':'))     {         hostAndPort = context.Request.UserHostName.Split(':');     }     else     {         hostAndPort = new string[] { context.Request.UserHostName, '80' };     }      IPHostEntry ipAddress = Dns.GetHostEntry(hostAndPort[0]);     IPEndPoint ip = new IPEndPoint(IPAddress.Parse(ipAddress.AddressList[0].ToString()), int.Parse(hostAndPort[1]));     socket = new Socket(ip.AddressFamily, SocketType.Stream, ProtocolType.Tcp);     socket.Connect(ip); 

BEGIN NEW CODE

Encoding ASCII = Encoding.ASCII; Byte[] byteGetString = ASCII.GetBytes(getString); Byte[] receiveByte = new Byte[256]; string response = string.Empty; socket.Send(byteGetString, byteGetString.Length, 0); Int32 bytes = socket.Receive(receiveByte, receiveByte.Length, 0); response += ASCII.GetString(receiveByte, 0, bytes); while (bytes > 0) { bytes = socket.Receive(receiveByte, receiveByte.Length, 0); strPage = strPage + ASCII.GetString(receiveByte, 0, bytes); } socket.Close();  string separator = '\r\n\r\n'; string header = strPage.Substring(0,strPage.IndexOf(separator)); string content = strPage.Remove(0, strPage.IndexOf(separator) + 4);  byte[] byteResponse = ASCII.GetBytes(content); context.Response.ContentLength64 = byteResponse .Length; context.Response.OutputStream.Write(byteResponse , 0, byteResponse .Length); context.Response.OutputStream.Close(); 

END NEW CODE

After connecting to the socket I don’t know how to get the Stream response to decompress, and send back to context.Response.OutputStream

Any help will be appreciated. Thanks. Cheers.

EDIT 2: With this edit now seems to be working fine (same as HttpWebRequest at least). Do you find any error here?

EDIT 3: False alarm… Still can’t get this working

EDIT 4: I needed to add the following lines to Scott’s code … because not always the first to bytes of reponseStream are the gzip magic number. The sequence seems to be: 0x0a (10), 0x1f (31), 0x8b (139). The last two are the gzip magic number. The first number was always before in my tests.

if (contentEncoding.Equals('gzip')) {     int magicNumber = 0;     while (magicNumber != 10)         magicNumber = responseStream.ReadByte();     responseStream = new GZipStream(responseStream, CompressionMode.Decompress); } 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T06:33:00+00:00Added an answer on May 11, 2026 at 6:33 am

    Here’s some code that works for me.

    using System; using System.Collections.Generic; using System.IO; using System.Net; using System.Net.Sockets; using System.Text; using System.IO.Compression;  namespace HttpUsingSockets {     public class Program {         private static readonly Encoding DefaultEncoding = Encoding.ASCII;         private static readonly byte[] LineTerminator = new byte[] { 13, 10 };          public static void Main(string[] args) {             var host = 'stackoverflow.com';             var url = '/questions/523930/sockets-in-c-how-to-get-the-response-stream';              IPHostEntry ipAddress = Dns.GetHostEntry(host);             var ip = new IPEndPoint(ipAddress.AddressList[0], 80);             using (var socket = new Socket(ip.AddressFamily, SocketType.Stream, ProtocolType.Tcp)) {                 socket.Connect(ip);                 using (var n = new NetworkStream(socket)) {                     SendRequest(n, new[] {'GET ' + url + ' HTTP/1.1', 'Host: ' + host, 'Connection: Close', 'Accept-Encoding: gzip'});                      var headers = new Dictionary<string, string>();                     while (true) {                         var line = ReadLine(n);                         if (line.Length == 0) {                             break;                         }                         int index = line.IndexOf(':');                         headers.Add(line.Substring(0, index), line.Substring(index + 2));                     }                      string contentEncoding;                     if (headers.TryGetValue('Content-Encoding', out contentEncoding)) {                         Stream responseStream = n;                         if (contentEncoding.Equals('gzip')) {                             responseStream = new GZipStream(responseStream, CompressionMode.Decompress);                         }                         else if (contentEncoding.Equals('deflate')) {                             responseStream = new DeflateStream(responseStream, CompressionMode.Decompress);                         }                          var memStream = new MemoryStream();                          var respBuffer = new byte[4096];                         try {                             int bytesRead = responseStream.Read(respBuffer, 0, respBuffer.Length);                             while (bytesRead > 0) {                                 memStream.Write(respBuffer, 0, bytesRead);                                 bytesRead = responseStream.Read(respBuffer, 0, respBuffer.Length);                             }                         }                         finally {                             responseStream.Close();                         }                          var body = DefaultEncoding.GetString(memStream.ToArray());                         Console.WriteLine(body);                     }                     else {                         while (true) {                             var line = ReadLine(n);                             if (line == null) {                                 break;                             }                             Console.WriteLine(line);                         }                     }                 }             }         }          static void SendRequest(Stream stream, IEnumerable<string> request) {             foreach (var r in request) {                 var data = DefaultEncoding.GetBytes(r);                 stream.Write(data, 0, data.Length);                 stream.Write(LineTerminator, 0, 2);             }             stream.Write(LineTerminator, 0, 2);             // Eat response             var response = ReadLine(stream);         }          static string ReadLine(Stream stream) {             var lineBuffer = new List<byte>();             while (true) {                 int b = stream.ReadByte();                 if (b == -1) {                     return null;                 }                 if (b == 10) {                     break;                 }                 if (b != 13) {                     lineBuffer.Add((byte)b);                 }             }             return DefaultEncoding.GetString(lineBuffer.ToArray());         }     } } 

    You could substitute this for the Socket/NetworkStream and save a bit of work.

    using (var client = new TcpClient(host, 80)) {       using (var n = client.GetStream()) {      } }  
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 120k
  • Answers 120k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer eval "$(alias -p|grep '^alias ls='|sed "s/'$/ -o'/")" Note that this… May 12, 2026 at 12:06 am
  • Editorial Team
    Editorial Team added an answer You can't, starting with Android 1.5. The most you can… May 12, 2026 at 12:06 am
  • Editorial Team
    Editorial Team added an answer I'd claim that this is flat out wrong except perhaps… May 12, 2026 at 12:06 am

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am currently running into a problem where an element is coming back from
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.