I am very sorry for the confusing. I was trying to be brief. Here is the
class, and I renamed the socket to client for easyier reading. I am using
TCPTrace at the moment to test this, and it gets and sends the proper
responses so I do not believe it is a Dos prevention of some kind.
using System;
using System.IO;
using System.Net.Sockets;
using System.Text;
namespace ReanneCorp.Library.V2.Common.WebUtils
{
public class PullWebsiteClass
{
private Uri websiteUri;
private TcpClient client = null;
private string results = "";
private int port;
private string host;
private string pathQuery;
public Uri WebsiteUri
{
get { return websiteUri; }
set { websiteUri = value; }
}
public TcpClient Client
{
get { return client; }
set { client = value; }
}
public NetworkStream Stream
{
get { return stream; }
set { stream = value; }
}
public string Results
{
get { return results; }
set { results = value; }
}
public PullWebsiteClass(Uri websiteUri)
{
this.websiteUri = websiteUri;
InitConnection();
}
private void InitConnection()
{
port = websiteUri.Port;
host = websiteUri.Host;
pathQuery = websiteUri.PathAndQuery;
client = new TcpClient();
client.ReceiveTimeout = 10000;
try
{
client.Connect(host, port);
}
catch(SocketException ex)
{
throw ex;
}
}
public void MakePageRequest()
{
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";
MemoryStream memoryStream = new MemoryStream();
Byte[] requestObject =
Encoding.ASCII.GetBytes(contents.ToCharArray());
try
{
stream = client.GetStream();
if (stream.CanWrite)
{
stream.Write(requestObject, 0, contents.Length);
}
if (stream.CanRead)
{
int data = stream.ReadByte();
while (data != -1)
{
memoryStream.WriteByte((byte) data);
data = stream.ReadByte();
}
results =
Encoding.ASCII.GetString(memoryStream.ToArray());
}
if (results.Contains("HTTP/1.1 302 Moved Temporarily"))
{
client.Close();
string redirect = results.Remove(0,
results.IndexOf("Location: ") + 10);
redirect = redirect.Substring(0,
redirect.IndexOf("Content-Length:") - 1).Trim();
PullWebsiteClass newRequest = new PullWebsiteClass(new
Uri(redirect));
newRequest.MakePageRequest();
results = newRequest.Results;
}
}
catch (Exception e)
{
throw e
}
finally
{
memoryStream.Close();
client.Close();
}
}
}
}
Here is the calling code:
[Test]
public void CheckHeaders()
{
PullWebsiteClass getPage = new PullWebsiteClass(new
Uri("http://localhost:8081/test.aspx"));
getPage.MakePageRequest();
string page = getPage.Results;
Console.WriteLine(page);
Assert.AreNotEqual(page.Length, 0);
}
"Peter Duniho" wrote:
On Fri, 29 Jun 2007 13:22:05 -0700, Pain and headache <Pain and
<he******@discussions.microsoft.com>wrote:
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 [...]
Some thoughts:
* You say you get an error trying to make the connection. But you
didn't bother to post any code related to creating or connecting your TCP
socket.
* As far as I know, there's no such thing as a TcpSocket, nor is there
a Socket.GetStream() method. You can create a Socket instance that
encapsulates a TCP socket, and you can create a NetworkStream instance
using a Socket instance. Is that what you did? If so, why does your post
and code say something else? And if not, what _did_ you do?
All that said, assuming your code normally works, you may be running into
some kind of anti-DoS defense, depending on how you're using the code. If
you are repeatedly trying to make the same request to the same HTTP
server, it may detect that case and stop responding, at least for some
time.
If you can post a concise-but-complete example of code, client _and_
server, that reliably reproduces the problem, someone here may be able to
provide better advice. Absent that, there's not much in your post to go
on, and in fact what you posted doesn't make much sense in the context of
regular .NET/C# programming (since you mention a class and a method that
are simply not present in the basic .NET Framework).
Pete