How to display image at client site??
-
Hi all, i made a simple proxyserver which can display basic HTML page at client site, but i have no idea why the image part of webpage always gone!! following is my simple code which accept the request from client site, then reponse.
int port = 8080; // Create server socket Socket proxyServer = new Socket(AddressFamily.InterNetwork, SocketType.Stream,ProtocolType.Tcp); proxyServer.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendTimeout, 5000); // call the Listen method to listen for and queue incoming connection requests. proxyServer.Bind( new IPEndPoint(GetHostIP(), port) ); proxyServer.Listen( port ); // Display start message Console.WriteLine("ProxyServer started at port " + port); Console.WriteLine("ProxyServer ready for accepting incoming connections..."); //endless loop while(true) { // waiting for client Socket clientConnect = proxyServer.Accept(); // Create the NetworkStream for communicating with the remote host. NetworkStream clientStream = new NetworkStream( clientConnect, true ); HttpWebRequest loHttp = (HttpWebRequest) WebRequest.Create("http://www.google.co.uk/intl/en_uk/images/logo.gif"); // Set properties - 10 secs loHttp.Timeout = 10000; // Retrieve request info headers HttpWebResponse loWebResponse = (HttpWebResponse) loHttp.GetResponse(); System.IO.StreamReader loResponseStream = new System.IO.StreamReader(loWebResponse.GetResponseStream() , System.Text.Encoding.UTF8); Byte[] buf = System.Text.Encoding.UTF8.GetBytes( loResponseStream.ReadToEnd() ); //Console.WriteLine(lcHtml); loWebResponse.Close(); loResponseStream.Close(); clientStream.Write(buf, 0, buf.Length); clientStream.Flush(); clientStream.Close(); }
plz help me out~~ many thanks! -
Hi all, i made a simple proxyserver which can display basic HTML page at client site, but i have no idea why the image part of webpage always gone!! following is my simple code which accept the request from client site, then reponse.
int port = 8080; // Create server socket Socket proxyServer = new Socket(AddressFamily.InterNetwork, SocketType.Stream,ProtocolType.Tcp); proxyServer.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendTimeout, 5000); // call the Listen method to listen for and queue incoming connection requests. proxyServer.Bind( new IPEndPoint(GetHostIP(), port) ); proxyServer.Listen( port ); // Display start message Console.WriteLine("ProxyServer started at port " + port); Console.WriteLine("ProxyServer ready for accepting incoming connections..."); //endless loop while(true) { // waiting for client Socket clientConnect = proxyServer.Accept(); // Create the NetworkStream for communicating with the remote host. NetworkStream clientStream = new NetworkStream( clientConnect, true ); HttpWebRequest loHttp = (HttpWebRequest) WebRequest.Create("http://www.google.co.uk/intl/en_uk/images/logo.gif"); // Set properties - 10 secs loHttp.Timeout = 10000; // Retrieve request info headers HttpWebResponse loWebResponse = (HttpWebResponse) loHttp.GetResponse(); System.IO.StreamReader loResponseStream = new System.IO.StreamReader(loWebResponse.GetResponseStream() , System.Text.Encoding.UTF8); Byte[] buf = System.Text.Encoding.UTF8.GetBytes( loResponseStream.ReadToEnd() ); //Console.WriteLine(lcHtml); loWebResponse.Close(); loResponseStream.Close(); clientStream.Write(buf, 0, buf.Length); clientStream.Flush(); clientStream.Close(); }
plz help me out~~ many thanks! -
Bluebamboo wrote:
plz help me out
Isolate the code that downloads the GIF file and unit test it.
Hi mike, Thanks for your reply,but i am not quite clear about what you said, do you mean i should save my code part in GIF format file then upload it? GIF? a picture?:confused:
-
Hi mike, Thanks for your reply,but i am not quite clear about what you said, do you mean i should save my code part in GIF format file then upload it? GIF? a picture?:confused:
The part that uses a HttpWebRequest to download the image file resulting in a byte array containing the image. That should be isolated and unit tested. The unit test could save the byte array to a disk file that can be verified to be correct. None of that has anything to do with your "proxyServer" and sending the byte array to the client socket. That is why it can and should be "isolated". Take some time to read about software design principles.
"Just about every question you've asked over the last 3-4 days has been "urgent". Perhaps a little planning would be helpful?"
Colin Angus Mackay in the C# forumled mike