本文介绍了在C#套接字:如何获得响应流?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图取代这个:

 无效的ProcessRequest(对象listenerContext)
{
    VAR上下文=(HttpListenerContext)listenerContext;
    乌里URL =新的URI(context.Request.RawUrl);
    HttpWebRequest.DefaultWebProxy = NULL;
    HttpWebRequest的HttpWebRequest的=(HttpWebRequest的)WebRequest.Create(URL);
    httpWebRequest.Method = context.Request.HttpMethod;
    httpWebRequest.Headers.Clear();
    如果(context.Request.UserAgent!= NULL)httpWebRequest.UserAgent = context.Request.UserAgent;
    的foreach(在context.Request.Headers.AllKeys串headerKey)
    {
        尝试{httpWebRequest.Headers.Set(headerKey,context.Request.Headers [headerKey]); }
            赶上(例外){}
    }    使用(HttpWebResponse httpWebResponse =(HttpWebResponse)httpWebRequest.GetResponse())
    {
        流responseStream = httpWebResponse.GetResponseStream();
        如果(httpWebResponse.ContentEncoding.ToLower()。包含(gzip的))
            responseStream =新GZipStream(responseStream,COM pressionMode.Decom preSS);
        否则,如果(httpWebResponse.ContentEncoding.ToLower()。包含(放气))
                responseStream =新DeflateStream(responseStream,COM pressionMode.Decom preSS);        MemoryStream的memStream =新的MemoryStream();        字节[] = respBuffer新的字节[4096];
        尝试
        {
            INT读取动作= responseStream.Read(respBuffer,0,respBuffer.Length);
            而(读取动作大于0)
            {
                memStream.Write(respBuffer,0,读取动作);
                读取动作= responseStream.Read(respBuffer,0,respBuffer.Length);
            }
        }
        最后
        {
            responseStream.Close();
        }        字节[]味精= memStream.ToArray();        context.Response.ContentLength64 = msg.Length;
        使用(流斯特劳特= context.Response.OutputStream)
        {
            strOut.Write(味精,0,msg.Length);
        }
    }
    赶上(异常前)
    {
        //一些错误处理
    }
}

与插座。这是我到目前为止有:

 无效的ProcessRequest(对象listenerContext)
{
    HttpListenerContext上下文=(HttpListenerContext)listenerContext;
    乌里URL =新的URI(context.Request.RawUrl);
    字符串的getString =的String.Format(GET {0} HTTP / 1.1 \\ r \\ n主机:{1} \\ r \\ nAccept编码:gzip \\ r \\ n \\ r \\ n,
                context.Request.Url.PathAndQuery,
                context.Request.UserHostName);    Socket套接字= NULL;    字符串[] hostAndPort;
    如果(context.Request.UserHostName.Contains(:))
    {
        hostAndPort = context.Request.UserHostName.Split(':');
    }
    其他
    {
        hostAndPort =新的String [] {context.Request.UserHostName,80后};
    }    IPHostEntry ip地址= Dns.GetHostEntry(hostAndPort [0]);
    IPEndPoint的ip =新IPEndPoint(IPAddress.Parse(ipAddress.AddressList [0]的ToString()),int.Parse(hostAndPort [1]));
    插座=新的Socket(ip.AddressFamily,SocketType.Stream,ProtocolType.Tcp);
    socket.Connect(IP);

开始新的code

 编码ASCII = Encoding.ASCII;
字节] byteGetString = ASCII.GetBytes(的getString);
字节] receiveByte =新的字节[256];
字符串的响应=的String.Empty;
socket.Send(byteGetString,byteGetString.Length,0);
INT32字节= socket.Receive(receiveByte,receiveByte.Length,0);
响应+ = ASCII.GetString(receiveByte,0,字节);
而(字节大于0)
{
字节= socket.Receive(receiveByte,receiveByte.Length,0);
strPage = strPage + ASCII.GetString(receiveByte,0,字节);
}
socket.Close();字符串分隔符=\\ r \\ n \\ r \\ n;
串标头= strPage.Substring(0,strPage.IndexOf(分离器));
串含量= strPage.Remove(0,strPage.IndexOf(隔板)+ 4);字节[] = byteResponse ASCII.GetBytes(内容);
context.Response.ContentLength64 = byteResponse。长度;
context.Response.OutputStream.Write(byteResponse,0,byteResponse。长度);
context.Response.OutputStream.Close();

END NEW code

连接到插座后,我不知道如何让流响应DECOM preSS,并发送回的 context.Response.OutputStream

任何帮助将AP preciated。
谢谢。
干杯。

编辑2:
有了这个编辑现在看来(至少相同的HttpWebRequest)是工作的罚款。你在这里找到任何错误?

修改3:
虚惊一场...仍不能得到这个工作。

修改4:
我需要添加以下行斯科特的code ...因为不是总是第一个reponseStream的字节是gzip的幻数。
该序列似乎是:0x0A的(10),0x1F的(31),0x8b(139)。最后两个是gzip的幻数。第一个数字是之前一直在我的测试。

 如果(contentEncoding.Equals(gzip的))
{
    INT魔术数字= 0;
    而(魔术数字!= 10)
        魔术数字= responseStream.ReadByte();
    responseStream =新GZipStream(responseStream,COM pressionMode.Decom preSS);
}


解决方案

下面是一些code,它为我的作品。

 使用系统;
使用System.Collections.Generic;
使用System.IO;
使用System.Net;
使用的System.Net.Sockets;
使用System.Text;
使用System.IO.Com pression;命名空间HttpUsingSockets {
    公共类节目{
        私人静态只读编码DefaultEncoding = Encoding.ASCII;
        私人静态只读的byte [] LineTerminator =新的字节[] {13,10};        公共静态无效的主要(字串[] args){
            VAR主机=stackoverflow.com;
            VAR URL =/问题/ 523930 /插座式-C-的how-to-GET-的响应流;            IPHostEntry ip地址= Dns.GetHostEntry(主机);
            变种的ip =新IPEndPoint(ipAddress.AddressList [0],80);
            使用(VAR插座=新的Socket(ip.AddressFamily,SocketType.Stream,ProtocolType.Tcp)){
                socket.Connect(IP);
                使用(VAR N =新的NetworkStream(插座)){
                    sendRequest将(N,新[] {GET+ URL +HTTP / 1.1,主持人:+主持人连接:关闭,接受编码:gzip});                    VAR标题=新词典<字符串,字符串>();
                    而(真){
                        VAR线=的ReadLine(N);
                        如果(line.Length == 0){
                            打破;
                        }
                        INT指数= line.IndexOf(':');
                        headers.Add(line.Substring(0,索引),line.Substring(指数+ 2));
                    }                    串contentEncoding;
                    如果(headers.TryGetValue(内容编码,出contentEncoding)){
                        流responseStream = N;
                        如果(contentEncoding.Equals(gzip的)){
                            responseStream =新GZipStream(responseStream,COM pressionMode.Decom preSS);
                        }
                        否则,如果(contentEncoding.Equals(放气)){
                            responseStream =新DeflateStream(responseStream,COM pressionMode.Decom preSS);
                        }                        VAR memStream =新的MemoryStream();                        VAR respBuffer =新的字节[4096];
                        尝试{
                            INT读取动作= responseStream.Read(respBuffer,0,respBuffer.Length);
                            而(读取动作大于0){
                                memStream.Write(respBuffer,0,读取动作);
                                读取动作= responseStream.Read(respBuffer,0,respBuffer.Length);
                            }
                        }
                        最后{
                            responseStream.Close();
                        }                        变种体= DefaultEncoding.GetString(memStream.ToArray());
                        Console.WriteLine(体);
                    }
                    其他{
                        而(真){
                            VAR线=的ReadLine(N);
                            如果(行== NULL){
                                打破;
                            }
                            Console.WriteLine(线);
                        }
                    }
                }
            }
        }        静态无效sendRequest将(流流的IEnumerable<串GT;要求){
            的foreach(在要求VAR R){
                VAR数据= DefaultEncoding.GetBytes(R);
                stream.Write(数据,0,data.Length);
                stream.Write(LineTerminator,0,2);
            }
            stream.Write(LineTerminator,0,2);
            //吃响应
            VAR响应=的ReadLine(流);
        }        静态字符串的ReadLine(流流){
            VAR lineBuffer =新的List<位>();
            而(真){
                INT B = stream.ReadByte();
                如果(二== -1){
                    返回null;
                }
                如果(二== 10){
                    打破;
                }
                如果(B!= 13){
                    lineBuffer.Add((字节),B);
                }
            }
            返回DefaultEncoding.GetString(lineBuffer.ToArray());
        }
    }
}

您可以替代本作的插座/的NetworkStream和节省一点的工作。

 使用(VAR的客户=新的TcpClient(主机,80)){
      使用(VAR N = client.GetStream()){
     }
}

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);
}
解决方案

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

这篇关于在C#套接字:如何获得响应流?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-08 01:59
查看更多