HttpWebRequest with response - vb.net

I want make an HttpWebRequest and get one response, this is my code.
Dim inStream As StreamReader
Dim webRequest As WebRequest
Dim webresponse As WebResponse
Dim request As HttpWebRequest = webRequest.Create("https:/x.x.x")
request.CookieContainer = New CookieContainer
request.Credentials = New NetworkCredential(login, "password")
Dim respone = request.GetResponse()
inStream = New StreamReader(respone.GetResponseStream())
TextBox1.Text = inStream.ReadToEnd
But I received 2 responses. One is okay but the second is without session. Can I filter the responses and get only one?

Related

VB POST request whit form-control values

I am having different problems when trying to make a POST request to my API, the API works perfectly tested in postman.
I would like to know if anyone sees that I am doing wrong, thank you very much
Dim url As String = "http://0.0.0.0/connect"
Dim dataToPassFromFormData As String = "client_id=1020&client_secret=FF29D58E&grant_type=password&username=admin&password=123"
Dim enc As UTF8Encoding
Dim postdatabytes As Byte()
Dim request As HttpWebRequest = TryCast(WebRequest.Create(url), HttpWebRequest)
enc = New System.Text.UTF8Encoding()
postdatabytes = enc.GetBytes(dataToPassFromFormData)
request.Method = "POST"
request.ContentLength = postdatabytes.Length
Using stream = request.GetRequestStream()
stream.Write(postdatabytes, 0, postdatabytes.Length)
End Using
Dim response As HttpWebResponse = TryCast(request.GetResponse(), HttpWebResponse)
Dim receiveStream As Stream = response.GetResponseStream()
Dim readStream As StreamReader = New StreamReader(receiveStream, Encoding.UTF8)
Dim us As user
us = JsonConvert.DeserializeObject(Of user)(readStream.ReadToEnd())
response.Close()
readStream.Close()
Return us.access_token
My api expects these parameters by body / form-data
client_id=1020
client_secret=FF29D58E
grant_type=password
username=admin
password=123

VB POST REQUEST

I am having different problems when trying to make a POST request to my API, the API works perfectly tested in postman.
I would like to know if anyone sees that I am doing wrong, thank you very much
Dim request As WebRequest = WebRequest.Create("http://0.0.0.0/connect")
request.Method = "POST"
Dim postData As String = "client_id=1&client_secret=F&grant_type=password&username=usuario&password=123&scope=Profile offline_access PaymentBO&provider=PersonalOld"
Dim postdatabytes As Byte() = Encoding.UTF8.GetBytes(postData)
request.ContentLength = postdatabytes.Length
Dim dataStream As Stream = request.GetRequestStream()
dataStream.Write(postdatabytes, 0, postdatabytes.Length)
dataStream.Close()
Dim response As WebResponse = request.GetResponse()
dataStream = response.GetResponseStream()
Dim reader As StreamReader = New StreamReader(dataStream)
Dim responseFromServer As String = reader.ReadToEnd()
response.Close()
Return responseFromServer
The api responds to me (attached image)

Cannot deserialise the JSON array

I'm working with VB.Net and can't manage to display the following JSON file in a datagridview.
{"files":[
{"file": "Test.out", "linecount": "4"},{"file": "test1.out", "linecount": "41"},{"file": "NocheinTest.out", "linecount": "41"}
]}
Can anyone help me with that?
here is the complete code
Dim request As HttpWebRequest
Dim response As HttpWebResponse = Nothing
Dim reader As StreamReader
Dim url As String
url = "http://...."
request = DirectCast(WebRequest.Create(url), HttpWebRequest)
response = DirectCast(request.GetResponse(), HttpWebResponse)
reader = New StreamReader(response.GetResponseStream())
Dim s As String
s = reader.ReadToEnd
Dim files = JObject.Parse(s)("files").ToDictionary(Function(jt) jt("file").ToString(), Function(jt) Convert.ToInt32(jt("linecount")))
DataGridView1.DataSource = files.ToList()

how to download a file with its cookie and URL?

I have a big problem downloading .xlsx files from a site automatically.
I have tried the following code and cookie is ready:
Dim request As HttpWebRequest = CType(WebRequest.Create(("http://www.trademap.org/Country_SelProduct_TS.aspx?nvpm=1|||||0101|||4|1|1|1|2|1|2|1|1")), HttpWebRequest)
request.CookieContainer = New CookieContainer()
Dim response As HttpWebResponse = CType(request.GetResponse(), HttpWebResponse)
For Each cook As Cookie In response.Cookies
msgbox(cook.value)
Next
I have the cookie but don't know how to download the file using the cookie and the URL without being asked. Can it be done by webclient.downloadfile or something like that?
Your second request has to use the same CookieContainer object to share its information.
Dim cookieContainer as New CookierContainer()
Dim request As HttpWebRequest = CType(WebRequest.Create(("http://www.trademap.org/Country_SelProduct_TS.aspx?nvpm=1|||||0101|||4|1|1|1|2|1|2|1|1")), HttpWebRequest)
request.CookieContainer = cookieContainer
Dim response As HttpWebResponse = CType(request.GetResponse(), HttpWebResponse)
Dim request2 as HttpWebRequest = CType(WebRequest.Create("second url"), HttpWebRequest);
request2.CookieContainer = cookieContainer
Dim response2 As HttpWebResponse = CType(request2.GetResponse(), HttpWebResponse)

VB.Net 401 Unauthorized HTTP Web Request

I'm attempting to login to a cPanel using a POST Request in VB.Net. I have the correct credentials when logging in and when posting I still get an 'Unauthorized (401)' response when it should be '301' (analysed using Tamper Data Firefox Add-On). Below is my post request information and function.
Private Function POSTreq(ByVal URL$, ByVal Data$)
Dim tempCookie As New CookieContainer
Dim DataBytes As Byte() = Encoding.ASCII.GetBytes(Data)
Dim Request As HttpWebRequest = TryCast(WebRequest.Create(URL), HttpWebRequest)
Request.Method = "POST"
Request.ContentType = "application/x-www-form-urlencoded"
Request.ContentLength = DataBytes.Length
Dim PostData As Stream = Request.GetRequestStream()
PostData.Write(DataBytes, 0, DataBytes.Length)
PostData.Close()
Dim Response As HttpWebResponse = Request.GetResponse()
Dim ResponseStream As Stream = Response.GetResponseStream()
Dim StreamReader As New StreamReader(ResponseStream)
Dim Text$ = StreamReader.ReadToEnd()
Return Text
End Function
Post URL
http://example.com:2082/login/
Post Data
login_theme=cpanel&user=USERNAME&pass=PASSWORD&goto_uri=%2F
I could reproduce your described behaviour with your code.
If I set the CookieContainer it works on my side, and I was able to log in:
rem ...
Request.CookieContainer = tempCookie
Request.Method = "POST"
rem ... and so on ...
The second Solution would be to just provide the Credentials:
rem ...
Dim myFullUri = new Uri(URL)
Dim myCredentials As New NetworkCredential(Username, Password)
Dim myCache As New CredentialCache()
rem Add the credentials for that specific host and
rem for "Basic" authentication only
myCache.Add(New Uri(myFullUri.Scheme & "://" & myFullUri.Authority), _
"Basic", myCredentials)
Request.Credentials = myCache
Request.CookieContainer = tempCookie
Request.Method = "POST"
rem ... and so on ...