G
Guest
Hi,
I am trying to get a webpage using a TcpSocket instead of a standard
Webrequest. Initial, it works fine but after the 2 or 3 request the
tcpclient I start to get the following error:
A connection attempt failed because the connected party did not properly
respond after a period of time, or established connection failed because
connected host has failed to respond
Here is the code making the call, this is the third iteration after trying
several others with out sucess:
string contents = "GET " + pathQuery + " HTTP/1.1\r\n" +
"Host: " + host + ":" + port + "\r\n" +
"User-Agent: Mozilla/5.0 (Windows; U; Windows
NT 6.0; en-US; rv:1.8.1.4) Gecko/20070515 Firefox/2.0.0.4\r\n" +
"Accept:
text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5\r\n" +
"Accept-Language: en-us,en;q=0.5\r\n" +
"Accept-Encoding: gzip,deflate\r\n" +
"Accept-Charset:
ISO-8859-1,utf-8;q=0.7,*;q=0.7\r\n" +
"Keep-Alive: 300\r\n" +
"Connection: keep-alive\r\n\r\n";
Byte[] requestObject = Encoding.ASCII.GetBytes(contents.ToCharArray());
try
{
stream = socket.GetStream();
if (stream.CanWrite)
{
stream.Write(requestObject, 0, contents.Length);
}
MemoryStream memoryStream = new MemoryStream();
if (stream.CanRead)
{
int data = stream.ReadByte();
while (data != -1)
{
memoryStream.WriteByte((byte) data);
data = stream.ReadByte();
}
results =
Encoding.ASCII.GetString(memoryStream.ToArray());
}
}
catch (Exception e)
{
Console.WriteLine(Encoding.ASCII.GetString(memoryStream.ToArray()));
throw e;
}
finally
{
stream.Close();
socket.Close();
}
I am trying to get a webpage using a TcpSocket instead of a standard
Webrequest. Initial, it works fine but after the 2 or 3 request the
tcpclient I start to get the following error:
A connection attempt failed because the connected party did not properly
respond after a period of time, or established connection failed because
connected host has failed to respond
Here is the code making the call, this is the third iteration after trying
several others with out sucess:
string contents = "GET " + pathQuery + " HTTP/1.1\r\n" +
"Host: " + host + ":" + port + "\r\n" +
"User-Agent: Mozilla/5.0 (Windows; U; Windows
NT 6.0; en-US; rv:1.8.1.4) Gecko/20070515 Firefox/2.0.0.4\r\n" +
"Accept:
text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5\r\n" +
"Accept-Language: en-us,en;q=0.5\r\n" +
"Accept-Encoding: gzip,deflate\r\n" +
"Accept-Charset:
ISO-8859-1,utf-8;q=0.7,*;q=0.7\r\n" +
"Keep-Alive: 300\r\n" +
"Connection: keep-alive\r\n\r\n";
Byte[] requestObject = Encoding.ASCII.GetBytes(contents.ToCharArray());
try
{
stream = socket.GetStream();
if (stream.CanWrite)
{
stream.Write(requestObject, 0, contents.Length);
}
MemoryStream memoryStream = new MemoryStream();
if (stream.CanRead)
{
int data = stream.ReadByte();
while (data != -1)
{
memoryStream.WriteByte((byte) data);
data = stream.ReadByte();
}
results =
Encoding.ASCII.GetString(memoryStream.ToArray());
}
}
catch (Exception e)
{
Console.WriteLine(Encoding.ASCII.GetString(memoryStream.ToArray()));
throw e;
}
finally
{
stream.Close();
socket.Close();
}