how to download a file with its cookie and URL? - vb.net

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)

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)

HttpWebRequest with response

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?

GetResponse not working for internal url

I am trying to return a webpage as a stream.
What i have got so far works when i try and access an external url i.e. 'http://www.google.com'. But when i try and access a website on our server i.e. 'http://servername/applicationname/default.aspx'.
Below is the code that i currently have, that works for external :
Dim request As HttpWebRequest = CType(WebRequest.Create("http://www.google.com/search?q=google"), HttpWebRequest)
Dim response As HttpWebResponse = CType(request.GetResponse, HttpWebResponse)
Dim resStream As Stream = response.GetResponseStream
But when i try it with this line i get The remote server returned an error: (401) Unauthorized.
Dim request As HttpWebRequest = CType(WebRequest.Create("http://Server/Application/SubFolder/testing.aspx"), HttpWebRequest)
Dim response As HttpWebResponse = CType(request.GetResponse(), HttpWebResponse)
I have tried altering the credentials it accesses the server with using :
request.Credentials = CredentialCache.DefaultCredentials
And i have tried using a web client :
Dim myWebClient As New WebClient()
Dim myStream As Stream = myWebClient.OpenRead("http://Server/Application/SubFolder/testing.aspx")
You're getting the 401 error because the web server's authentication is rejecting the request. You may need to explicitly set your credentials.
request.Credentials = New NetworkCredential("userName", "password", "domain")

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 ...