Jan 8, 2015 09:34 AM
(This post was last modified: Jan 8, 2015 09:34 AM by DBots.)
I started a small project today which is a wrapper for the HttpWebRequest/HttpWebResponse synchronous methods.
I kept it very basic because I wanted it to be easy to use without having so many variables etc. so pretty much the guidelines I kept were:
1. Did the request go through fine?
-If yes, work with the response.
2.If the request errored, was it a timeout?
-If so, change proxy, etc, and reissue the request.
-If not, check the exception or log it, or change proxy, etc, and reissue the request.
This is the code that I use to send and receive a request:
Now as you can see HttpRequest.Get/Post are shared functions which take in ClientProperties byref.
Is it bad practice to pass byref on a shared function? Is this even threadsafe?
Here is the code for the classes:
http://pastebin.com/GwaysRUn
I kept it very basic because I wanted it to be easy to use without having so many variables etc. so pretty much the guidelines I kept were:
1. Did the request go through fine?
-If yes, work with the response.
2.If the request errored, was it a timeout?
-If so, change proxy, etc, and reissue the request.
-If not, check the exception or log it, or change proxy, etc, and reissue the request.
This is the code that I use to send and receive a request:
PHP Code
<?php
Dim clientProperties As New HttpProperties
With clientProperties
.Timeout = 100000
.ReadWriteTimeout = 100000
.MaximumRedirections = 7
'.ProxyConnection = True
'.Proxy = New WebProxy("127.0.0.1:8080")
.Url = New Uri("http://www.google.com")
.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"
.AcceptEncoding = "gzip, deflate, sdch"
.AcceptLanguage = "en-US,en;q=0.8"
.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36"
End With
Using response As HttpResponse = HttpRequest.Get(clientProperties)
If Not response.Success Then
If response.TimedOut Then
Console.WriteLine("Time out message: " & response.Exception.Message)
Else
Console.WriteLine("Error: " & response.Exception.Message)
Console.WriteLine("Body:{0}{1}", Environment.NewLine, HttpRequest.ReadAsString(response))
End If
Else
Console.Write("Headers:{0}{1}", Environment.NewLine, HttpRequest.ReadHeaders(response))
Dim str As String = HttpRequest.ReadAsString(response)
If str.Length = 0 Then
Console.WriteLine("Empty Body..")
Else
Console.WriteLine(str)
End If
End If
End Using
Now as you can see HttpRequest.Get/Post are shared functions which take in ClientProperties byref.
Is it bad practice to pass byref on a shared function? Is this even threadsafe?
Here is the code for the classes:
http://pastebin.com/GwaysRUn



![[Image: 86251a37e635a21aea7eb7e014b0d820.gif]](https://i.gyazo.com/86251a37e635a21aea7eb7e014b0d820.gif)