11

我正在尝试替换它:

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
    }
}

带插座。这是我到目前为止所拥有的:

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);

开始新代码

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();

结束新代码

连接到套接字后,我不知道如何让 Stream 响应解压,并发送回context.Response.OutputStream

任何帮助将不胜感激。谢谢。干杯。

编辑 2: 现在这个编辑似乎工作正常(至少与 HttpWebRequest 相同)。你发现这里有什么错误吗?

编辑 3: 误报...仍然无法正常工作

编辑 4: 我需要在 Scott 的代码中添加以下几行......因为并非总是第一个字节 reponseStream 是 gzip 幻数。顺序似乎是:0x0a(10),0x1f(31),0x8b(139)。最后两个是 gzip 幻数。在我的测试中,第一个数字总是在前面。

if (contentEncoding.Equals("gzip"))
{
    int magicNumber = 0;
    while (magicNumber != 10)
        magicNumber = responseStream.ReadByte();
    responseStream = new GZipStream(responseStream, CompressionMode.Decompress);
}
4

3 回答 3

11

这是一些对我有用的代码。

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());
        }
    }
}

您可以用它代替 Socket/NetworkStream 并节省一些工作。

using (var client = new TcpClient(host, 80)) {
      using (var n = client.GetStream()) {
     }
} 
于 2009-02-08T23:13:35.210 回答
4

Socket,顾名思义,是访问网络的底层。您甚至可以将数据报协议与套接字一起使用。在这种情况下,流根本没有意义。

虽然我不确定您为什么要做 HttpWebRequest 轻松完成的工作,但要将数据读/写到套接字,您可以使用 Send/Receive 方法。如果你想像访问 TCP 套接字一样有流,你应该使用 TcpClient/TcpListener 类来包装套接字并为其提供网络流。

于 2009-02-07T15:46:56.420 回答
3

还有以 Socket 作为参数的 NetworkStream 类。;)

于 2009-02-07T18:05:37.107 回答