Hack Forums
[.NET] EasyHttp | HttpWebRequests Made Easy | GET & POST - 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: [.NET] EasyHttp | HttpWebRequests Made Easy | GET & POST (/showthread.php?tid=4826710)



EasyHttp | HttpWebRequests Made Easy | GET & POST - Punkbastard - May 18, 2015

Hi guys,

Pretty simple and short but definitely useful for some members struggling with HttpWebRequests (GET & POST request).

I created a simple class to make HttpWebRequests easier, it has the capabilities to perform GET and POST request and return the source.

It takes cares of the cookies so it can be used for logins.

Source: http://pastebin.com/ZVmjseGg

Usage:
Code
' Create new instance of EasyHttp
Dim easyhttp As New EasyHttp()

' Do a GET request and get the source of it in a string
If (easyhttp.HttpGet("http://www.google.com")) Then
    ' GET request successful
    Dim source As String = easyhttp.GetSource()
Else
    MessageBox.Show("GET Request failed, error: " & easyhttp.GetError())
End If

' Do a POST request to some uri
If (easyhttp.HttpPost("http://www.example.com", "username=admin&password=SecretPassword12345)) Then
    ' POST request successful
    Dim source As String = easyhttp.GetSource()
Else
    MessageBox.Show("POST Request failed, error: " & easyhttp.GetError())
End If

Note:
I coded this in like half an hour so I can still contain bugs, I did my best and if you find anything do not hesitate to notice me, I'll improve the code than.

Thanks.


* - nathan72389 - May 18, 2015

you know you can just use a web client to GET/POST data also right?

all of the webclient download functions are GET requests
and the upload functions are POST requests.


* - Punkbastard - May 18, 2015

(May 18, 2015 01:00 PM)nathan72389 Wrote:  you know you can just use a web client to GET/POST data also right?

all of the webclient download functions are GET requests
and the upload functions are POST requests.

Yes I know, but if you need to store cookies I guess you'll need to manually populate the Cookie header before making the request and then pull out the Set-Cookies header on the response?
I find the HttpWebRequests less limited although it's sometimes hard to code.

But if you just have a look at my source, is there anything to do better?


* - SIM-1676753 - May 18, 2015

(May 18, 2015 01:16 PM)Punkbastard Wrote:  Yes I know, but if you need to store cookies I guess you'll need to manually populate the Cookie header before making the request and then pull out the Set-Cookies header on the response?
I find the HttpWebRequests less limited although it's sometimes hard to code.

But if you just have a look at my source, is there anything to do better?

You can always inherit the WebClient and have a CookieContainer property.


* - LanMan - May 18, 2015

Or you can use HttpClient, it use a shared cookie container/cache/credentials by default, is async and can make con­cur­rent requests

HttpClient client = new HttpClient();
HttpResponseMessage response = await client.GetAsync("http://www.google.com");
string content = await response.Content.ReadAsStringAsync();


* - SIM-1676753 - May 18, 2015

(May 18, 2015 08:59 PM)LanMan Wrote:  Or you can use HttpClient, it use a shared cookie container/cache/credentials by default, is async and can make con­cur­rent requests

HttpClient client = new HttpClient();
HttpResponseMessage response = await client.GetAsync("http://www.google.com");
string content = await response.Content.ReadAsStringAsync();

Yessir. The HttpClient is absolutely beautiful.