Hi everyone,
I have a problem with reading a socket for the 2nd time. I'll explain.
By using TcpClient I set up a connection to a webserver. Then I perform a GET request and read it out. When I do another GET request afterwards, that works, but I can't read the socket.
Somebody pls help me out.
If you find other small thingies in the code that are wrong you can tell me too. I'm just a beginner in C#.
Thanks in advance!
TcpClient oTcpClient = new TcpClient();
IAsyncResult oIAsyncResult = oTcpClient.BeginConnect("www.example.com", 80, null, null);
bool success = oIAsyncResult.AsyncWaitHandle.WaitOne(5000, true);
if (!success)
{
oTcpClient.Close();
Console.WriteLine("Timeout...");
return;
}
oTcpClient.EndConnect(oIAsyncResult);
NetworkStream oNetworkStream = oTcpClient.GetStream();
StreamReader oStreamReader = new StreamReader(oNetworkStream);
StreamWriter oStreamWriter = new StreamWriter(oNetworkStream);
oStreamWriter.AutoFlush = true;
oStreamWriter.Write("GET /index.php HTTP/1.1\r\n"
+ "Host: www.example.com\r\n"
+ "User-Agent: Mozilla/5.0\r\n"
+ "Connection: close\r\n"
+ "\r\n");
string output = "";
string src = "";
while (output != null)
{
output = oStreamReader.ReadLine();
if (output != null)
{
src += output;
}
}
//here I do stuff with the src
oStreamWriter.Write("GET /index.php HTTP/1.1\r\n"
+ "Host: www.example.com\r\n"
+ "User-Agent: Mozilla/5.0\r\n"
+ "Connection: close\r\n"
+ "\r\n");
//starting from here it goes wrong
string output = "";
string src = "";
while (output != null)
{
output = oStreamReader.ReadLine();
if (output != null)
{
src += output;
}
}