private void GetBody(System.Web.HttpContext context) { CustomTracer.Write("HttpSimpleSocket GetBody: establishing a TcpClient connection", context); GetConnection(); CustomTracer.Write("HttpSimpleSocket GetBody: got a connection", context); string request = "GET " + path + " HTTP/1.1\r\n" + "Host: " + host + ":" + port + "\r\n\r\n" + "Connection : close\r\n\r\n"; GetResponse(context, request); ExtractResponse(); CustomTracer.Write("HttpSimpleSocket GetBody: done reading response", context); }
//grab the entire raw response //this may not be the best way to do this but the response is puuled in two formats: //ASCII and binary. We use the ascii copy to search the headers and chunks. Some utf-8 //text may get mangled but atleast the character count is preserved. If we converted to //UTF-8, the text would look good but some multi-byte characters would distort the character //count. We use the binary data to parse out the needed chunks and then convert that to //UTF-8 which gets passed back to the client. private void GetResponse(System.Web.HttpContext context, string strReq) { NetworkStream ns = client.GetStream(); Stream stream = null; if (useSsl) { var sslStream = new SslStream(ns); sslStream.AuthenticateAsClient(host); stream = sslStream; } else { stream = new BufferedStream(ns); } CustomTracer.Write("HttpSimpleSocket GetBody: got a response", context); ArrayList lst = new ArrayList(); StringBuilder strBuff = new StringBuilder(); try { byte[] bytes = System.Text.Encoding.UTF8.GetBytes(strReq); stream.Write(bytes, 0, bytes.Length); buff = new byte[1024]; int bytesRead = 0; try { while ((bytesRead = stream.Read(buff, 0, buff.Length)) > 0) { byte[] cpy = new byte[bytesRead]; Array.Copy(buff, cpy, bytesRead); lst.AddRange(cpy); strBuff.Append(Encoding.ASCII.GetString(buff, 0, bytesRead)); } } catch (System.IO.IOException ioe) { Exception inner = ioe.InnerException; if (inner != null && inner is SocketException) { throw ioe.InnerException; } else { throw ioe; } } buff = (byte[])lst.ToArray(Type.GetType("System.Byte")); } finally { if (stream != null) { stream.Close(); } client.Close(); } body = strBuff.ToString(); }