Getting "The underlying connection was closed: An unexpected error occurred on a send" on GetReponse() but not via browser - vb.net

I am attempting to utilize a REST service that uses a GET method. I'm using the .Net Framework 4.5.2. What I've written below is just a test to see if I can actually make the request. If I put the URL directly into a browser I get a good response back with string data in json format. But when I attempt to run the code I end up getting the following error back:
"The underlying connection was closed: An unexpected error occurred on a send."
InnerException = {"Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host."}
I have tried setting the keepalive to both true and false and I've tried using a WebClient along with DownloadString too....all result in the same error. Does anyone have any idea of what I'm missing? Or is this a problem somehow on the server side?
Dim script As String = "236"
Dim deploy As String = "1"
Dim compid As String = "915960_SB2"
Dim h As String = "value1"
Dim id As String = "1241389"
Dim status As String = "in freezer"
Dim request As HttpWebRequest
Dim response As HttpWebResponse
Try
'I have removed the real website name from this code.
Dim theurl As String = "https://website/app/site/hosting/scriptlet.nl?script={0}&deploy={1}&compid={2}&h={3}&Id={4}&Status={5}"
Dim url As String = String.Format(theurl, script, deploy, compid, h, id, status)
request = HttpWebRequest.Create(url)
request.Method = "GET"
request.ContentType = "application/json"
'This is where the error occurs
response = request.GetResponse()
Catch ex As Exception
Dim test as string=""
Finally
End Try

I solved it. I stumbled upon the following web site where someone else had been having the same issue.
http://www.avivroth.com/2013/05/02/rest-calls-in-net-c-over-ssl-https/
The solution is to set the security protocol. I just went down the list from ssl to tls11 then to tls12 and found that the last one worked.
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12

Related

VB.NET The remote server returned an error: (405) Method Not Allowed

The below code works on some URLs, but some other URLs having parameters return the error: The remote server returned an error: (405) Method Not Allowed.
my work:
Dim objHttpWebRequest As HttpWebRequest = Nothing
Dim objHttpWebResponse As HttpWebResponse = Nothing
Dim objRequestStream As Stream = Nothing
Dim objResponseStream As Stream = Nothing
Dim objXMLReader As XmlTextReader
Try
objHttpWebRequest = WebRequest.Create(URL)
'Start HttpRequest
objHttpWebRequest.Method = "POST"
objHttpWebRequest.ContentType = "application/xml"
'Get Stream Object
objRequestStream = objHttpWebRequest.GetRequestStream()
objRequestStream.Close()
'Start HTTP Response
objHttpWebResponse = objHttpWebRequest.GetResponse()
If objHttpWebResponse.StatusCode = HttpStatusCode.OK Then
objResponseStream = objHttpWebResponse.GetResponseStream()
objXMLReader = New XmlTextReader(objResponseStream)
Dim xmldoc As XmlDocument = New XmlDocument
xmldoc.Load(objXMLReader)
XMLResponse = xmldoc
objXMLReader.Close()
End If
Is the problem in the method I am using? or the content type?
Based on the status code, the problem is in the method. Not all of the URLs might be able to respond to POST requests.
Wikipedia states
405 Method Not Allowed
A request was made of a resource using a request method not supported
by that resource; for example, using GET on a form which requires data
to be presented via POST, or using PUT on a read-only resource.

WebClient.UploadData "The underlying connection was closed"

I'm trying to upload a file from an FTP site to Basecamp using the Basecamp API. I'm using a simple console application. Here's my code:
Try
Dim accountID As String = ConfigurationManager.AppSettings("BaseCampID")
Dim projectID As Integer = 9999999
Dim folderName As String = "XXXXX/XXXXX"
Dim fileName As String = "XXX.zip"
'The URL to access the attachment method of the API
Dim apiURL = String.Format("https://basecamp.com/{0}/api/v1/projects/{1}/attachments.json", accountID, projectID)
'Get the file from the FTP server as a byte array
Dim fileBytes As Byte() = GetFileBytes(String.Format("{0}\\{1}", folderName, fileName))
'Initialize the WebClient object
Dim client As New WebClient()
client.Headers.Add("Content-Type", "application/zip")
'Need to provide a user-agent with a URL or email address
client.Headers.Add("User-Agent", "Basecamp Upload (email#email.com)")
'Keep the connection alive so it doesn't close
client.Headers.Add("Keep-Alive", "true")
'Provide the Basecamp credentials
client.Credentials = New NetworkCredential("username", "password")
'Upload the file as a byte array to the API, and get the response
Dim responseStr As Byte() = client.UploadData(apiURL, "POST", fileBytes)
'Convert the JSON response to a BaseCampAttachment object
Dim attachment As BaseCampAttachment
attachment = JSonHelper.FromJSon(Of BaseCampAttachment)(Encoding.Default.GetString(responseStr))
Catch ex As Exception
Console.WriteLine(ex.Message)
Finally
Console.ReadLine()
End Try
But whenever it calls client.UploadData, I get the error message "The underlying connection was closed: The connection was closed unexpectedly." I ran into this issue earlier and thought I solved it by adding the "Keep-Alive" header, but it's not working anymore. The API works if I upload a local file with client.UploadFile, but I'd like to just upload the file from they byte array from the FTP rather than downloading the file locally then uploading it to Basecamp.
Any thoughts would be greatly appreciated. Thanks!
I never figured out what was wrong with the WebClient call, but I ended up using a Basecamp API wrapper from https://basecampwrapper.codeplex.com. That wrapper uses HTTPRequest and HTTPResponse instead of WebClient.UploadData. It's also much easier to just use that wrapper than to try writing my own code from scratch.

VB.NET | HttpWebRequest, How to see if the host is online?

I am coding a whitelist for my application in VB.NET.
I am using the HttpWebRequest Method and HttpWebResponse.
If the Whitelist Host is down, the whitelist is bypassed and the program is available to anyone which is a vulnerability.
Public Function GetWhitelist(ByVal PageURL As String) As String
Dim S As String = ""
Try
Dim Request As HttpWebRequest = WebRequest.Create("WHITELIST URL HERE")
Dim Response As HttpWebResponse = Request.GetResponse()
Using Reader As StreamReader = New StreamReader(Response.GetResponseStream())
S = Reader.ReadToEnd
End Using
Catch ex As Exception
Debug.WriteLine("Start Program Error. Handle:0")
End Try
Return S
End Function
I want to give the user an error if the website is down, any ideas?
Regards.
What is your error condition? This function alone doesn't prevent anything from happening in the event of an exception. The function still exits with returning a string. So any consuming code would have to look for error conditions and handle them accordingly.
If the request fails, you should meaningfully handle the exception. Two ideas off the top of my head would include:
1) Let the exception bubble up the stack:
Public Function GetWhitelist(ByVal PageURL As String) As String
Dim S As String = ""
Dim Request As HttpWebRequest = WebRequest.Create("WHITELIST URL HERE")
Dim Response As HttpWebResponse = Request.GetResponse()
Using Reader As StreamReader = New StreamReader(Response.GetResponseStream())
S = Reader.ReadToEnd
End Using
Return S
End Function
This would make the attempt to contact the host and, if that attempt failed, throw an exception instead of return a string.
2) Throw a custom exception:
Public Function GetWhitelist(ByVal PageURL As String) As String
Dim S As String = ""
Try
Dim Request As HttpWebRequest = WebRequest.Create("WHITELIST URL HERE")
Dim Response As HttpWebResponse = Request.GetResponse()
Using Reader As StreamReader = New StreamReader(Response.GetResponseStream())
S = Reader.ReadToEnd
End Using
Catch ex As Exception
Debug.WriteLine("Start Program Error. Handle:0")
Throw New SomeCustomException(String.Format("Unable to contact host: {0}", PageURL), ex)
End Try
Return S
End Function
This would provide a more targeted exception instead of whatever comes out of the response reader, would provide useful runtime information about the error for logging and analysis (namely the runtime value of the PageURL), and takes a step toward hiding the implementation details from code outside of this object (since that code doesn't really care about the HttpWebRequest and HttpWebResponse, it just wants to know if the URL is good or not).
Remember that throwing an exception is a perfectly acceptable exit path for a function. It doesn't always have to return a value. An exception is an appropriate way of indicating an error condition. Your current implementation, however, "swallows" the exception and provides no indication to the consuming code that anything went wrong. Instead it returns a "magic value" of String.Empty which consuming code may or may not ignore.
Change your exception handler. The correct way to do this is to just try the request and handle the exception if it fails.

Getting multiple SQL Reports with HttpWebRequest - 401 Unauthorized

I am trying to call multiple SQL Reporting Services Reports, get the .pdfs back, then append them together. I have no problems appending .pdfs, but I'm getting an error on the second report. Here's some example code:
Dim httpReq As System.Net.HttpWebRequest = DirectCast(System.Net.WebRequest.Create(strLink), System.Net.HttpWebRequest)
Dim creds As New System.Net.NetworkCredential
creds.Domain = "MyDomain"
creds.UserName = "UserName"
creds.Password = "Password"
httpReq.Credentials = creds
Dim httpResp As System.Net.HttpWebResponse = DirectCast(httpReq.GetResponse(), System.Net.HttpWebResponse)
httpResp.Close()
httpReq = Nothing
httpResp = Nothing
Dim SecondHttpReq As System.Net.HttpWebRequest = DirectCast(System.Net.WebRequest.Create(strSecondLink), System.Net.HttpWebRequest)
SecondHttpReq.Credentials = creds
Dim SecondHttpResp As System.Net.HttpWebResponse = DirectCast(SecondHttpReq.GetResponse(), System.Net.HttpWebResponse)
When I try to create the second HttpResponse, I get a 401 Unauthorized error. When I check the SecondHttpReq object when it errors, I find that the Credentials property has been set back to Nothing. I've tried using a CookieContainer to no avail. I'd really rather not have to write a parent/sub report to get these together, so I'd like to hear other options. Thanks in advance.
Fixed. Someone changed permissions on our report server without notifying anyone.

How to use HttpWebRequest to download file

Trying to download file in code.
Current code:
Dim uri As New UriBuilder
uri.UserName = "xxx"
uri.Password = "xxx"
uri.Host = "xxx"
uri.Path = "xxx.aspx?q=65"
Dim request As HttpWebRequest = DirectCast(WebRequest.Create(uri.Uri), HttpWebRequest)
request.AllowAutoRedirect = True
request = DirectCast(WebRequest.Create(DownloadUrlIn), HttpWebRequest)
request.Timeout = 10000
'request.AllowWriteStreamBuffering = True
Dim response As HttpWebResponse = Nothing
response = DirectCast(request.GetResponse(), HttpWebResponse)
Dim s As Stream = response.GetResponseStream()
'Write to disk
Dim fs As New FileStream("c:\xxx.pdf", FileMode.Create)
Dim read As Byte() = New Byte(255) {}
Dim count As Integer = s.Read(read, 0, read.Length)
While count > 0
fs.Write(read, 0, count)
count = s.Read(read, 0, read.Length)
End While
'Close everything
fs.Close()
s.Close()
response.Close()
Running this code and checking the response.ResponseUri indicates im being redirected back to the login page and not to the pdf file.
For some reason its not authorising access what could I be missing as Im sending the user name and password in the uri? Thanks for your help
You don't need all of that code to download a file from the net
just use the WebClient class and its DownloadFile method
you should check and see if the site requires cookies (most do), i'd use a packet analyzer and run your code and see exactly what the server is returning. use fiddler or http analyzer to log packets
With UWP, this has become a more pertinent question as UWP does not have a WebClient. The correct answer to this question is if you are being re-directed to the login page, then there must be an issue with your credentials OR the setting (or lack of) header for the HttpWebRequest.
According to Microsoft, the request for downloading is sent with the call to GetResponse() on the HttpWebRequest, therefore the downloaded file SHOULD be in the stream in the response (returned by the GetResponse() call mentioned above).