Hack Forums
[C#] WebRequestExtension - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [C#] WebRequestExtension (/showthread.php?tid=5530456)



WebRequestExtension - Ninja - Jan 21, 2017

Since I see a lot of people still use the old and deprecated WebRequest (yes it still has its uses sometimes, but there are better alternatives) I decided to release this. It is an extension method so adding headers and setting postdata becomes much easier and you can send a request in as little as 2 lines.

https://gist.github.com/GekkeVent/c3b8766e271a5741be474041c428f9a8

Example
Code
NameValueCollection headers = new NameValueCollection()
{
    {"Accept", "*/*"}
};
HttpWebRequest request = WebRequest.CreateHttp("url");
request.SetHeaders(headers);
string source = request.GetSource();



* - teh_distance - Jan 21, 2017

oooh very useful, I also made something like this almost a month ago.


* - Cecilia - Jan 21, 2017

You should add an override for SetPost to be compatible with a NameValueCollection...
That'd be nice to see.


* - Ninja - Jan 21, 2017

(Jan 21, 2017 10:46 PM)LaPanthere Wrote:  You should add an override for SetPost to be compatible with a NameValueCollection...
That'd be nice to see.

Never thought about that. How about FormUrlEncodedContent though?


* - Cecilia - Jan 21, 2017

(Jan 21, 2017 10:49 PM)Ninja Wrote:  Never thought about that. How about FormUrlEncodedContent though?

I don't think that's available in 4.0 (sadface).
I guess you can use that :/


* - Dialog - Jan 22, 2017

HttpWebRequest isn't deprecated, only its constructors are. See: https://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.aspx#Anchor_6

A good alternative is HttpClient, though.


* - Ninja - Jan 22, 2017

(Jan 22, 2017 05:10 AM)Dialog Wrote:  HttpWebRequest isn't deprecated, only its constructors are. See: https://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.aspx#Anchor_6

A good alternative is HttpClient, though.

What I mean is its old. There are better alternatives like WebClient and HttpClient (only in 4.0+).


* - JJack - Jan 23, 2017

Thank you for the code snippet. I didn't know about this method.


* - abratar - May 13, 2017

what happend? i was looking for a web request class and remembered that you made one and now that i need it its gone...


* - Ninja - May 13, 2017

(May 13, 2017 08:17 AM)abratar Wrote:  what happend? i was looking for a web request class and remembered that you made one and now that i need it its gone...

I dont know what happened to the link. Here it is again:
https://gist.github.com/GekkeVent/c3b8766e271a5741be474041c428f9a8


* - RyanR45 - May 15, 2017

(May 13, 2017 06:18 PM)Ninja Wrote:  I dont know what happened to the link. Here it is again:

hey can you PM me your skype, im interested in your services


* - Ast0r - May 16, 2017

(Jan 22, 2017 09:10 PM)Ninja Wrote:  What I mean is its old. There are better alternatives like WebClient and HttpClient (only in 4.0+).

Depending on the task, WebClient may not be able to do it but WebRequest can.🤝


* - Ninja - May 16, 2017

(May 16, 2017 06:01 AM)Ast0r Wrote:  Depending on the task, WebClient may not be able to do it but WebRequest can.🤝

I do agree that WebRequests allows for easier 'low level' control over the WebClient. You can however inherit WebClient and add cookie support and what not.


* - HFDone - May 16, 2017

I do agree that WebRequests allows for easier 'low level' control over the webrequest 😧


* - Ninja - May 16, 2017

(May 16, 2017 08:45 AM)HFDone Wrote:  I do agree that WebRequests allows for easier 'low level' control over the webrequest 😧

Meant WebClient. Fixed it.