I am working on a program in VS 2017 coding in VB.Net. The program downloads Web pages using Net.WebClient.DownloadString and then parses the data. It worked fine for a year or more then one day I started getting an exception when downloading the pages.
The ex.Message is: 'The underlying connection was closed: An unexpected error occurred on a send.'
The ex.InnerException.Message is: 'Unable to write data to the transport connection: A request to send or receive data was disallowed because the socket is not connected and (when sending on a datagram socket using a sendto call) no address was supplied.'
I have VS 2017 installed on 2 other PC's at this location and on 1 at another location. They all continue to run the code without exception. It is only an issue on this PC.
The code is below (I changed the Web address but it fails for any valid URL).
Any ideas why this fails on my main PC only?
Public Function DownloadData() As Boolean
Dim strURL As String = "https://www.google.com/"
Dim strOutput As String
Try
Using WC As New Net.WebClient
strOutput = WC.DownloadString(strURL)
If strOutput.Length > 0 Then
If ParseData(strOutput) = True Then
Return True
End If
Else
Return False
End If
End Using
Catch ex As Exception
MessageBox.Show(ex.InnerException.Message, "Error")
End Try
End Function
Related
I have the following function in a VB.NET program that was working for a connection with an outsource system XML-API for years with no problems.
Recently, the responsible team for the specific API moved their system to another address (and probably another system) and claim that there is no change and I will just have to make a change in the address that I connect to.
I changed the address and firstly I had some connectivity problems with the new address ("An existing connection was forcibly closed by the remote host") and discovered that I had to compile my code to an earlier .NET Framework version in order to connect because the authentication protocol from their side was changed to TLS v1.2 instead of TLS v1.1 that they were using in the old address.
Now I get an exception “unsupported media type” from CookComputing.XmlRpcV2 when I try to communicate (fetch data) from the new API.
With some googling, I found that the specific exception has to do with content-type header of the request.
The strange thing is that using Postman I can succesfully get a response with both the old and new address using the same content-type (application/xml). The response from the API team was that their system is set to accept and produce the following content types: text/plain, text/html, and application/xml, so I don't think that the problem is with the content-type header.
Does anyone have any ideas on what else should I try ?
Imports CookComputing.XmlRpc
Imports System.Net
Public Shared Function CommunicateWithApi(ByVal pTest As Boolean,
ByVal pCommand As String,
ByVal pInParamsArray As ArrayList,
ByRef pOutParamsArray As ArrayList,
ByRef pErrorMessage As String) As Boolean
Try
Dim _Api = XmlRpcProxyGen.Create(Of IActionAid)()
If pTest Then
_Api.Url = "https://test.blabla.org/servlet/B2BNK"
Else
_Api.Url = "https://live.blabla.org/servlet/B2BNK"
End If
_Api.XmlEncoding = System.Text.Encoding.GetEncoding("ISO-8859-7")
_Api.XmlRpcMethod = pCommand
_Api.UseIndentation = False
_Api.UseIntTag = True
Dim pParams() As Object = pInParamsArray.ToArray
Dim Response() As Object = {}
Response = _Api.FetchData(pParams)
For i As Integer = 0 To Response.Count - 1
pOutParamsArray.Add(Response(i))
Next
Return True
Catch fex As XmlRpcFaultException
MsgBox((String.Format("Fault Response: {0} ", fex.FaultCode)) + fex.FaultString, MsgBoxStyle.Critical)
pErrorMessage = String.Format("{0:00000000000000000000},{1}", fex.FaultCode, fex.FaultString)
Return False
Catch webEx As WebException
MsgBox("WebException: " + webEx.Message, MsgBoxStyle.Critical)
pErrorMessage = String.Format("70000000000000000000,{0}", webEx.Message)
If webEx.Response IsNot Nothing Then
webEx.Response.Close()
End If
Return False
Catch xmlRpcEx As XmlRpcServerException
MsgBox("XmlRpcServerException: " + xmlRpcEx.Message, MsgBoxStyle.Critical)
pErrorMessage = String.Format("80000000000000000000,{0}", xmlRpcEx.Message)
Return False
Catch defEx As Exception
MsgBox("Exception: " + defEx.Message, MsgBoxStyle.Critical)
pErrorMessage = String.Format("90000000000000000000,{0}", defEx.Message)
Return False
End Try
End Function
I have a named pipes server running on a workstation. When the server receives a message I am able to process it. The problem is several messages can be sent at the same time. The server processes the first message but the remaining messages are lost. Not sure how to listen for multiple messages. I cannot batch the messages into one message as I need to have the server respond to each and every message separately. Does anyone know how I can accomplish this. My server code is below which is running in a thread.
Dim Pipe As New Pipes.NamedPipeServerStream("bellhop", Pipes.PipeDirection.InOut, -1, Pipes.PipeTransmissionMode.Message, Pipes.PipeOptions.Asynchronous)
While BellhopExiting = False
Pipe.WaitForConnection()
Dim Reader As New StreamReader(Pipe)
Dim XML As String = Reader.ReadLine
If XML IsNot Nothing Then
Dim Writer As New StreamWriter(Pipe)
Writer.AutoFlush = True
Try
ParseXML(XML)
'Send a success acknowledgement back to the sender.
Writer.WriteLine("Message Delivered")
Catch ex As Exception
'Send a failed acknowledgement back to the sender.
Writer.WriteLine("Failed->Bellhop->WaitForConnection->Computer: " & ComputerName & " Error: " & ex.Message)
End Try
End If
Pipe.Disconnect()
End While
I'm creating desktop aplication and when I write a username in TextBox1 and on Button1.Click event it should check does folder on web exists.
As far I've tried this:
username = Me.TextBox1.Text
password = Me.TextBox2.Text
Dim dir As Boolean = IO.Directory.Exists("http://www.mywebsite.com/" + username)
If dir = true Then
Dim response As String = web.DownloadString("http://www.mywebsite.com/" + username + "/Password.txt")
If response.Contains(password) Then
MsgBox("You've logged in succesfully", MsgBoxStyle.Information)
Exit Sub
Else
MsgBox("Password is incorect!")
End If
Else
MsgBox("Username is wrong, try again!")
End If
First problem is that my boolean is giving FALSE as answer (directory exists for sure and all permissions are granted to see folder). I tried to solve that with setting dir = false and after that I go into first IF (but that's not what I want, since it should be TRUE, not FALSE)
There we come to second problem, in this line: Dim response As String=web.DownloadString("http://www.mywebsite.com/" + username + "/Password.txt") I get this error message: The remote server returned an error: (404) Not Found.
Anyone more experienced with this kind of things who can help me?
IO.Directory.Exists will not work in this case. That method only works to check for a folder on a disk somewhere (locally or network) ; you can't use it to check for the existence of a resource over HTTP. (i.e a URI)
But even if it did work this way, it's actually pointless to call it before attempting to download - the method DownloadString will throw an exception if something goes wrong - as you have seen, in this case it's telling you 404 Not Found which means "This resource does not exist as far as you are concerned". **
So you should try/catch the operation, you need to catch exceptions of type WebException, cast its Response member to HttpWebException, and check the StatusCode property.
An good example (albeit in C#) is here
** I say "as far as you are concerned" because for all you know, the resource may very well exist on the server, but it has decided to hide it from you because you do not have access to it, etc, and the developer of that site decided to return 404 in this case instead of 401 Unauthorised. The point being that from your point of view, the resource is not available.
Update:
here is the code from the answer I linked to, translated via this online tool because my VB is dodgy enough :). This code runs just fine for me in LinqPad, and produces the output "testlozinka"
Sub Main
Try
Dim myString As String
Using wc As New WebClient()
myString = wc.DownloadString("http://dota2world.org/HDS/Klijenti/TestKlijent/password.txt")
Console.WriteLine(myString)
End Using
Catch ex As WebException
Console.WriteLine(ex.ToString())
If ex.Status = WebExceptionStatus.ProtocolError AndAlso ex.Response IsNot Nothing Then
Dim resp = DirectCast(ex.Response, HttpWebResponse)
If resp.StatusCode = HttpStatusCode.NotFound Then
' HTTP 404
'the page was not found, continue with next in the for loop
Console.WriteLine("Page not found")
End If
End If
'throw any other exception - this should not occur
Throw
End Try
End Sub
Hope that helps.
I am new to programming in vb.net. I have come a long ways in my development and understanding of vb, but there is one hurtle I can not seem to fix. I am hosting an ftp server on my pc and I am making an app for it to connect to my server and download files. The problem with all the sample code is that everyone ASSUMES the server WILL be ONLINE. My pc may not be running 24/7 and I also may not have the ftp service running.In the first case it shouldnt even register that it is connected. In the second case, it WILL say that is connected b/c the pc is on, but it will return that the machine ou are trying to connect to is actively refusing the connection. Is there a way to TRULY check if the program is indeed connected to the server WITHOUT generating a bunch of Exceptions in the debugger? All I want is a call like:
Dim ftponline As Boolean = False 'Set default to false
ftponline = checkftp()
If ftponline Then
'continue program
Else
'try a different server
End If
So it would be a function called checkftp that returns a boolean value of true or false.
Here is my info:
Using Visual Studio 2010 Pro
Using .Net framework 4
Can anyone help?
Thanks!
I have tried the rebex ftp pack as well as the Ultimate FTP Pack.
Here is the updated code:
Public Function CheckConnection(address As String) As Boolean
Dim logonServer As New System.Net.Sockets.TcpClient()
Try
logonServer.Connect(address, 21)
Catch generatedExceptionName As Exception
MessageBox.Show("Failed to connect to: " & address)
End Try
If logonServer.Connected Then
MessageBox.Show("Connected to: " & address)
Return True
logonServer.Close()
Else
Return False
End If
End Function
Public Sub ConnectFtp()
types.Clear()
models.Clear()
ListBox1.Items.Clear()
ListBox2.Items.Clear()
TextBox2.Clear()
Dim request As New Rebex.Net.Ftp
If CheckConnection(*) Then
Dim tempString As String()
request.Connect(*)
request.Login(*, *)
request.ChangeDirectory("/atc3/HD_Models")
Dim list As Array
list = request.GetNameList()
Dim item As String = ""
For Each item In list
tempString = item.Split(New Char() {" "c})
If types.Contains(tempString(0)) = False Then
types.Add(tempString(0))
End If
If models.Contains(item) = False Then
models.Add(item)
End If
Next
request.Disconnect()
request.Dispose()
ElseIf CheckConnection(*) Then
request.Connect(*)
request.Login(*, *)
request.ChangeDirectory(*)
Dim list2 As Array
list2 = request.GetNameList()
Dim item2 As String = ""
Dim tempString2 As String()
For Each item2 In list2
MessageBox.Show(item2)
tempString2 = item2.Split(New Char() {" "c})
If types.Contains(tempString2(0)) = False Then
types.Add(tempString2(0))
End If
If models.Contains(item2) = False Then
models.Add(item2)
End If
Next
request.Disconnect()
request.Dispose()
End If
End Sub
No matter what I do, the second server will not connect. I even put a messagebox to show what items were being returned in the second server, but there are no messageboxes apearing when I run the program with my server offline. Is there anyone who can help?
If your code is designed with proper exception catching, it shouldn't be generating a "bunch" of exceptions. The first exception you catch should be your indication that the connection failed and your code should cease attempting to communicate at that point. If for some reason you really need to check the connectivity before attempting the FTP connection, you should be able to simply attempt to synchronously open a TCP socket to the FTP server's port. If that works, it's up and running.
You could simply open a socket to the server's IP address on Port 21 (assuming default FTP port).
I'm not much of a VB.Net programmer, but here's a link to sample code:
http://vb.net-informations.com/communications/vb.net_Client_Socket.htm
If you can establish the socket connection, you know that something is listening on that port (though you have not yet proven it's an FTP server, or that it will accept your login credentials...).
If you wish to simply avoid exceptions in the debugger, you could place the connection code in a method and apply the DebuggerHidden attribute to that method.
I have an application was working fine and all the email functionality was working fine.
From yesterday, I started getting below error
Error Message:Service not available, closing transmission channel. The server response was: 4.3.2 Service not available, closing transmission channel
My VB.net code for sendmail is given below:
Public Sub SendMessage(ByVal toAddress As String, ByVal ccAddress As String)
Try
Dim message As New MailMessage()
Dim client As New SmtpClient()
'Set the sender's address
message.From = New MailAddress(fromAddress)
If (toAddress.Trim.Length > 0) Then
For Each addr As String In toAddress.Split(";"c)
message.To.Add(New MailAddress(addr))
Next
End If
If (ccAddress.Trim.Length > 0) Then
For Each addr As String In ccAddress.Split(";"c)
message.CC.Add(New MailAddress(addr))
Next
End If
message.BodyEncoding = Encoding.UTF8
message.Subject = Subject
message.Body = Body
message.IsBodyHtml = True
client.Send(message)
Catch ex As Exception
ErrorHandler.WriteError(ex.Message)
End Try
End Sub
Please suggest what can be cause behind this error and do let me know how can I fix this issue.
There isn't anything wrong with your code. This part of the error message:
4.3.2 Service not available, closing transmission channel
Is actually coming from your mail server, and the framework is simply passing the error message on to your application, and throwing it as part of the exception.
4.x.x errors are usually temporary, and are meant to be retried. Typically mail servers are overloaded when they throw a 400 error.
Check your email and see if it starts working.
Instead of (or in addition to) smtp authentication, some email servers will allow mail to be sent for 30 minutes (or some length) after a client computer checks POP email. If this is the case, it can make an application that does not use smtp authentication appear work sometimes and not work sometimes, with no change in the code.