2010-01-05 1 views
0

나는 Urkraine에서 왔고 나쁜 영어를 가지고있다. 그러나 어쨌든 내 질문에 대한 답이 있는지 확실하지 않다.Visual C#의 소켓. 도움이 필요하다!

[여기] [1]에서 예제를 취했지만 GZip 마법 번호가 유효하지 않은 예외가 있습니다. 이유는 무엇입니까? 이 라인

   string str = encoding.GetString(memStream.ToArray()); 
       ManageCookies(headers["Set-Cookie"], _headers["Host"]); 
       cachedFile = encoding.GetBytes(str); 

에서

public long WriteUrl() 
    { 
     long num1 = 0; 
     bool saveItAtCache = false; 
     bool existsAtCache = false; 
     byte[] cachedFile = null; 
     string ext = Path.GetExtension(_url).ToLower(); 
     if (!_url.Contains(".php") && ".gif.jpg.swf.js.css.png.html".IndexOf(ext) != -1 && ext != "") 
     { 
      saveItAtCache = true; 
      cachedFile = cache.GetFile(_url); 
      existsAtCache = (cachedFile != null); 
     } 
     if (existsAtCache) 
     { 
      writeSuccess(cachedFile.Length, null); 
      socket.Send(cachedFile); 
     } 
     string host = new Uri(_url).Host; 
     IPHostEntry ipAddress = Dns.GetHostEntry(host); 
     IPEndPoint ip = new IPEndPoint(ipAddress.AddressList[0], 80); 
     using (Socket s = new Socket(ip.AddressFamily, SocketType.Stream, ProtocolType.Tcp)) 
     { 
      s.Connect(ip); 
      using (NetworkStream n = new NetworkStream(s)) 
      { 
       if (HttpRequestType == "GET") 
       { 
        SendRequest(n, new[] { socketQuery}); 
       } 
       Dictionary<string, string> headers = new Dictionary<string, string>(); 
       while (true) 
       { 
        string line = ReadLine(n); 
        if (line.Length == 0) 
        { 
         break; 
        } 
        int index = line.IndexOf(':'); 
        if (!headers.ContainsKey(line.Substring(0, index))) 
        { 
         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, true); 
        } 
        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); 
         //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(); 
        } 
        string str = encoding.GetString(memStream.ToArray()); 
        ManageCookies(headers["Set-Cookie"], _headers["Host"]); 
        cachedFile = encoding.GetBytes(str); 
        if (saveItAtCache) 
        { 
         cache.Store(_url, cachedFile); 
        } 
        writeSuccess(cachedFile.Length, headers["Set-Cookie"]); 
        socket.Send(cachedFile); 
        num1 = str.Length; 
       } 
       else 
       { 
        while (true) 
        { 
         string line = ReadLine(n); 
         if (line == null) 
         { 
          break; 
         } 
         num1 = line.Length; 
        } 
       } 
      } 
     } 

     return num1; 
    } 
+0

귀하의 링크가이 질문에서 작동하지 않았습니다. – GrayWizardx

+0

클라이언트 요청의 형식이 올바른 이유는 무엇입니까? 어떤 클라이언트가 GZIP를 사용하여 업로드합니까? – EricLaw

답변

0

당신은 바이트 배열의 뒤쪽으로 바이트 배열을 문자열로 변환하고 있습니다. 원래 데이터는 gzip 또는 jpg이거나 실제로는 문자열이 아니기 때문에이 변환은 아마 그것을 망칠 것입니다. str을 사용하는 것을 볼 수 없으므로 str.Length 대신 길이가 필요할 때 cachedFile.Length를 사용하십시오.

관련 문제