I am trying to make a request to a website with a TcpClient, but I can't get it to work. First I make a connection with the remoteHost and write the request headers that are in the requestStr-variable. After that I am reading from the remote stream and expecting to get a response, but I don't get anything. I use the code from the while loop also for receiving request headers from a browser that uses my app as a proxy and that works fine.
The Eol variable is \r\n.
The requestStr variable is:
GET http://steam.com/ HTTP/1.1
Host: steam.com
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:44.0) Gecko/20100101 Firefox/44.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: nl,en-US;q=0.7,en;q=0.3
Accept-Encoding: gzip, deflate
Connection: keep-alive
The code snippit:
TcpClient remoteClient = new TcpClient(request.GetRemoteHost(), 80);
byte[] remoteRequest = Encoding.UTF8.GetBytes(requestStr);
NetworkStream remoteStream = remoteClient.GetStream();
remoteStream.Write(remoteRequest, 0, remoteRequest.Length);
remoteStream.Flush();
int j;
string remoteStr = "";
byte[] remoteBuffer = new byte[App.BufferSize];
recvRequest = true;
while (recvRequest && (j = remoteStream.Read(remoteBuffer, 0, remoteBuffer.Length)) != 0) {
string receivedData = Encoding.ASCII.GetString(remoteBuffer, 0, j);
if (receivedData.IndexOf(Eol) < 0) {
remoteStr += receivedData;
} else {
remoteStr += receivedData.Substring(0, receivedData.IndexOf(Eol + Eol));
recvRequest = false;
}
}
MessageBox.Show(remoteStr);