GetResponse not working for internal url - vb.net

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")

Related

Could not create SSL/TLS secure channel for HttpWebRequest - even after use SecurityProtocolType.Tls12

We are consuming one Rest API where host server configures on TLS 1.2, so for connect this api using below code but still receive "Exception - Could not create SSL/TLS secure channel". This will work properly in postman and Firefox RestClient.
**ServicePointManager.MaxServicePointIdleTime = 1000
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12**
Dim POSTRequest As HttpWebRequest = DirectCast(WebRequest.Create(url), HttpWebRequest)
POSTRequest.Method = "POST"
POSTRequest.ContentType = "application/json"
POSTRequest.ContentLength = postdata.Length
Dim tmpSource As Byte() = ASCIIEncoding.ASCII.GetBytes(postdata)
Dim POSTstream As Stream = POSTRequest.GetRequestStream()
POSTstream.Write(tmpSource, 0, postdata.Length)
Dim POSTResponse As HttpWebResponse = DirectCast(POSTRequest.GetResponse(), HttpWebResponse)
It will be very helpful if someone point me out on right direction.

Can't make BigCommerce API call in VB.NET

I get
'underlying connection was closed'
when running the code below. I am using vb.net 2012 (I must use this version) with the RestSharp library and am trying to retrieve product data from a bigcommerce.com store. This is a simple vb.net 2012 console program that once I get working I can build upon. I have tried changing around the code somewhat even making certain things redundant like the method and URL but I can't get it to work.
Dim client As New RestClient
client.BaseUrl = New Uri("https://api.bigcommerce.com/stores/mystorehash/v3/catalog/products")
Dim request As New RestRequest("https://api.bigcommerce.com/stores/mystorehash/v3/catalog/products", Method.GET)
request.AddHeader("Accept", "application/json")
request.AddHeader("Content-Type", "application/json")
request.AddHeader("X-Auth-Client", "notactualvaluenotactualvalue")
request.AddHeader("X-Auth-Token", "notactualvaluenotactualvalue")
request.Method = Method.GET
Dim response As New RestResponse
response = client.ExecuteAsGet(request, Method.GET)
Console.WriteLine("response.Content=" & response.Content)
Console.WriteLine("response.ErrorMessage=" & response.ErrorMessage)
Console.WriteLine("response.ResponseStatus=" & response.ResponseStatus)
Console.WriteLine("response.IsSuccessful=" & response.IsSuccessful)
Console.WriteLine("response.Headers.Count=" & response.Headers.Count)
Output:
Any help would be appreciated, hopefully I'm doing something stupid that can be easily fixed
For anyone else that finds this the full implementation of Nathan Booker's suggestion is below. When you attempt to access the Big Commerce api in a VB.NET application you need to specify TLS 1.2. If you don't you'll recieve an HTTP status of 502 - System.IO.IOException Authentication failed because the remote party has closed the transport stream. The solve is done like:
ServicePointManager.Expect100Continue = True
ServicePointManager.SecurityProtocol = CType(3072, SecurityProtocolType)
ServicePointManager.DefaultConnectionLimit = 9999
Dim request As HttpWebRequest = CType(WebRequest.Create("https://api.bigcommerce.com/stores/<Redacted>/v3/catalog/products"), HttpWebRequest)
request.AllowAutoRedirect = True
request.ContentType = "application/json"
request.Accept = "application/json"
request.Method = "GET"
request.Headers.Add("X-Auth-Client", "<Redacted>")
request.Headers.Add("X-Auth-Token", "<Redacted>")
Dim response As WebResponse = request.GetResponse()
Diagnostics.Debug.WriteLine((CType(response, HttpWebResponse)).StatusDescription)
Dim dataStream As Stream = response.GetResponseStream()
Dim reader As StreamReader = New StreamReader(dataStream)
Dim responseFromServer As String = reader.ReadToEnd()
Diagnostics.Debug.WriteLine(responseFromServer)
reader.Close()
response.Close()
This may be related to your HTTP version or (more likely) SSL/TLS protocol.
If possible, please make sure you're using HTTP 1.1 and TLS 1.2.

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)

Trouble with POSTing JSON to endpoint

I'm trying to submit a POST request containing JSON to a given endpoint. However the below code sends me to a login page which is the default functionality for all non-/api endpoints. Because Postman doesn't direct me to the login page but correctly hits the API, I'm assuming the problem is in this C# code.
Dim RemoteUrl As String = "https://my-site.com/api/tournament/results"
Dim result As String = ""
Dim xmlHttpReq As HttpWebRequest = CType(WebRequest.Create(RemoteUrl), HttpWebRequest)
xmlHttpReq.Method = "POST"
xmlHttpReq.ContentType = "application/json"
With (New StreamWriter(xmlHttpReq.GetRequestStream))
.Write(Json)
.Flush()
.Close()
Dim httpResponse As HttpWebResponse = CType(xmlHttpReq.GetResponse(), HttpWebResponse)
With (New StreamReader(httpResponse.GetResponseStream))
result = .ReadToEnd
End With
End With
Any idea what steps I need to take to debug this?

WebRequest Connection Error Only When Server Executing Backend Work

I've got a VB.NET app executing a simple WebRequest that is to interact with a web API. Through testing, I can successfully have the WebRequest executed and get a server response when I'm calling any static page on the server.
But when I execute a request of the API (which is interacting with a MySQL DB) the VB.NET app always throws this error:
The underlying connection was closed: An unexpected error occurred on
a send.
I do not get that error when I execute the same URL in a browser from the same PC. The API returns JSON encoded text with a text/plain content header.
Here is the VB.NET application code:
strURL = "https://soc-server.com/wireless/soc_wapi.php?&function=add_kb_event&logtype=33&datetime=2013-12-16%2013:50:50&kid=4&pid=1&peid=5&cid=97"
Dim ReturnValue As String = String.Empty
Dim Request As HttpWebRequest = CType(WebRequest.Create(New Uri(strURL)), HttpWebRequest)
Try
Dim ResponseText As String = String.Empty
Using Response As HttpWebResponse = CType(Request.GetResponse, HttpWebResponse)
Using ReceiveStream As Stream = Response.GetResponseStream
Using ReadStream As StreamReader = New StreamReader(ReceiveStream)
ReturnValue = ReadStream.ReadToEnd
processLog(ReturnValue)
End Using
End Using
End Using
Catch ex As Exception
processError(ex, "Error sending data")
End Try