Quantcast

Jump to content


Photo

[C#] HTTP Header Generator


  • Please log in to reply
5 replies to this topic

#1 Dan

Dan
  • Resident Know-It-All

  • 6382 posts


Users Awards

Posted 06 November 2009 - 07:42 AM

Seeing as I'm not going to be using this in NCC anymore (switched from TcpClient to HttpWebRequest), I thought someone may get some use out of it.

This class will generate GET or POST headers for you - very quickly and without the speed decrease of concatenating strings - there is absolutely no concatenation in this class.
StringBuilder and string.Format have both been proven to be faster than concatenation, and when you're generating headers, you need this speed.

http://pastebin.com/f34b0e695

Parameters:

string file - File (i.e. "/index.phtml")
string host - Host (i.e. "www.neopets.com")
string referer - The referring web page, cannot be left blank
string customUserAgent - a custom user agent to use instead of FF 3.5.4
bool gzip - Whether to accept a gzip response
string cookies - Any cookies to be sent with the request

string postData (POST only) - Post Data to send (i.e. "username=neocodex&password=neocodex")

Usage:
var headerGenerator = new HTTPHeaderGenerator();

var requestHeaders = headerGenerator.CreateGETHeaders("/index.phtml", "www.neopets.com", "http://www.google.co.uk", "*custom user agent*", false, "");

Oh - I tested this with a StopWatch() in C# - and it came back with a timing of 0 milliseconds. I assume I've broken into the microsecond barrier here, so you can guess that it's pretty fast :p

#2 ((_Blade_))

((_Blade_))
  • 20 posts

Posted 06 November 2009 - 08:25 AM

May I ask why you dropped TCPClient?

#3 Dan

Dan
  • Resident Know-It-All

  • 6382 posts


Users Awards

Posted 06 November 2009 - 08:33 AM

May I ask why you dropped TCPClient?


I was having a massive amount of trouble with the NetworkStreams - unhandled exceptions inside controls when reading the buffer byte sizing, etc.
Using HttpWebRequest now which seems to be doing the job excellently.

Any comments about the above code?

#4 ((_Blade_))

((_Blade_))
  • 20 posts

Posted 06 November 2009 - 08:51 AM

I was having a massive amount of trouble with the NetworkStreams - unhandled exceptions inside controls when reading the buffer byte sizing, etc.
Using HttpWebRequest now which seems to be doing the job excellently.

Only problem I ever had with NetworkStreams was it throwing "Request Timeout" exceptions. Other than that it has worked flawlessly for me :)

Any comments about the above code?

Damn thing looks fast :) It's always good to use the StringBuilder class when dealing with large strings :D

EDIT: Why not merge the Create[GET/POST]Header functions together? They share 90% of the same code :/ Just an observation :D

Edited by ((_Blade_)), 06 November 2009 - 09:04 AM.


#5 Dan

Dan
  • Resident Know-It-All

  • 6382 posts


Users Awards

Posted 06 November 2009 - 09:10 AM

Only problem I ever had with NetworkStreams was it throwing "Request Timeout" exceptions. Other than that it has worked flawlessly for me :)


Damn thing looks fast :) It's always good to use the StringBuilder class when dealing with large strings :D

EDIT: Why not merge the Create[GET/POST]Header functions together? They share 90% of the same code :/ Just an observation :D


Yeah, I had serious issues with the NetworkStreams - perhaps I was doing it wrong, but on my time-tests so far the difference between HttpWebRequest and TcpClient are extremely minimal..

Cheers, yeah - StringBuilder is great.

--

I could merge them together, yeah - but it means I'd have to implement if statements or ternary operators - which would slow things down further.

#6 Adam

Adam
  • Coffee God


  • 4771 posts


Users Awards

Posted 06 November 2009 - 10:35 AM

I'm really excited for this, from all the functions you told me about this NCC will be amazing :D.


1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users