Unable to resolve hostname over vpn - c#

I've been trying get my app to connect to a WCF service within the corporate network.
We use a VPN to keep everything somewhat hidden and secure.
I've tried several different methods and I'm unable to get the phone to connect to the service.
I first added the service as a service reference, and built the client out with the correct URL, but this just did nothing and then failed with a nondescript error message after around 50sec.
I then switched to System.Net.HttpClient. This again failed after around 50sec, but this time it threw a System.Net.WebException with the message "A server with the specified hostname could not be found".
I finally tried the ModernHttpClient as well, and this had the same result as before.
(Note that I tried the first two in a test console app project on my local machine first to make sure that they would complete a request successfully)
To make sure I wasn't going crazy and that the VPN was correctly resolving the name, I created a test page within the app which solely has a WebView on it, with the source set to the service url.
I opened the app and navigated to the test page and it loaded the service definition page without a problem.
for reference, this is the current code I have using ModernHttpClient:
using (var client = new HttpClient(new NativeMessageHandler()))
{
const string soap = "<soap msg>";
client.DefaultRequestHeaders.Add("SOAPAction", "<service namespace>");
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("text/xml"));
using (var response = await client.PostAsync("http://<url>/Service.svc", new StringContent(soap, Encoding.UTF8, "text/xml")))
{
using (var content = response.Content)
{
// parse the xml result
}
}
}
Is there something special that I should be doing to tell the HttpClient that it has to use the VPN? I thought that this was handled automatically.

I looked into the VPN configuration to make sure it was correct.
I looked into the DNS configuration to make sure it resolves correctly.
I looked into the phone's configuration to make sure it was correct.
I eventually found the issue is a problem with Airwatch's Tunnel VPN.
The VPN only works the first time the app is launched.
If the VPN remains open and is not closed before the app is restarted, then it will not allow connections to pass through.
Expecting a patch form airwatch soon that should (hopefully) rectify the issue!

Related

uwp, c# Dns.GetHostAddresses() not resolve hostname from hosts file

I am working on uwp app. I edited hosts file and redirecting www.example.com to local server IP(192.168.1.187). In browser website is loading properly but in application
System.Net.Dns.GetHostAddresses("https://www.exaple.com")[0]
Throws System.Net.Sockets.SocketException
If I remove http/https, it is working
System.Net.Dns.GetHostAddresses("www.exaple.com")[0]
output => 192.168.1.187
I want to specifically make a request to https://www.example.com.
System.Net.Dns.GetHostAddresses does hostname resolution. It is very similar to ping in command line. You can test, that using
ping www.example.com
Will give you response while
ping https://www.example.com
will not.
As for the fact that you cannot issue a HttpClient request to http://www.example.com, it might be because you don't have the appropriate capability set. Go to Package.appxmanifest, click the Capabilities tab and check the Private Networks (Client & Server) capability.

Azure Webjob: An attempt was made to access a socket in a way forbidden by its access permissions x.x.x.x:80

I know there's a couple questions so far involving this error - but nobody has generated this error yet due to a webjob on Azure.
My code is in the form of a C# Console Application. The main part of my code:
HttpClient client = new HttpClient();
var byteArray = Encoding.ASCII.GetBytes("user:password");
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray));
HttpResponseMessage response = await client.GetAsync("http://xx.xx.xx.xxx/cgi-bin/snapshot.cgi?channel=0");
byte[] myBytes = await response.Content.ReadAsByteArrayAsync();
string convertedFromString = Convert.ToBase64String(myBytes);
(The code above is called by the Main method)
If executed on my local machine it works just fine. If I add this code to an API method used by an App Service (part of an ASP.Net Core project), it also works.
However, when uploaded as an Azure webjob, I get this error:
An error occurred while sending the request.
Unable to connect to the remote server
An attempt was made to access a socket in a way forbidden by its
access permissions xx.xx.xx.xxx:80
Any ideas on how to get past this? I'm looking for any possible solution/work-around.
An attempt was made to access a socket in a way forbidden by its access permissions xx.xx.xx.xxx:80
Per my understanding, I assumed that you may hit the outgoing connections limitation from Azure Web App sandbox. Also, I would recommend that you could scale your app hosting plan to larger instance for narrowing this issue.
Additionally, here are some similar issues, you could refer to them:
Intermittent crashes in Azure Web Application
Starving outgoing connections on Windows Azure Web Sites

'No connection could be made because the target machine actively refused it'

I am working on a 'Smart Device Project' using .Net Framework 3.5. I am trying to connect to some Java SOAP services on a remote server.
In order to do that, I added 'Web References' to my project.
When I try to call my web service I get a WebException 'Unable to connect to the remote server' with the inner exception being 'No connection could be made because the target machine actively refused it'.
I searched quite a lot on the Web and StackOverflow and found a lot of ASP configuration and 'Unavaliable port' answers, but as I have another application using the exact same Service successfully, I can't get why the new one isn't getting through (It did sometimes through my tests so I suppose my client implementation isn't that bad)
I tried to look if there was some connection issue on the port by using some TcpClient:
System.Net.Sockets.TcpClient client = new System.Net.Sockets.TcpClient();
try
{
client.Connect("myServerName", 8087);
MessageBox.Show("Success");
} catch (Exception ex)
{
MessageBox.Show("Failure");
}
finally
{
client.Close();
}
This connection succeed.
Here is a sample on how I call my WebService:
WSServiceExtended srv = new WSServiceExtended();
srv.Proxy = new System.Net.WebProxy();
ServeurWSI wsi = new ServeurWSI();
srv.Url = "http://myServerName:8087/myServerApp/services/myService";
wsr = srv.login(wsi);
The service is called 'Extended' because I overrided the auto-generated one in order to add Cookie managment since I am using the Compact Framework. Following the sample in this thread:
https://social.msdn.microsoft.com/Forums/en-US/34d88228-0b68-4fda-a8cd-58efe6b47958/no-cookies-sessionstate-in-compact-framework?forum=vssmartdevicesvbcs
EDIT:
I made some new tests with the Web references and got it to work.
When I add the Web Reference, I have to put some Url to the Web Service. When I set it with the actual hostname instead of the 'localhost' everything is fine.
But then, since I set it manually to the real address just before the call, it shouldn't matter
srv.Url = "http://myServerName:8087/myServerApp/services/myService";
EDIT2:
I might have forgotten some specifics about my environnement.
The Web Services are exposed on my computer on some Tomcat Server.
The application I am working on is also developped on this computer (That's why I can add Web References by putting 'localhost' in the address)
The application is then deployed on a distant device (Windows CE) that will make calls the Web Services through WIFI (There, localhost wouldn't work then)
I tried calling the Web services from other computers successfully.
I'm beginning to think that there might be some differential between the called Url and the one that is set, otherwise, how would I have a difference in behaviour such as the one described in the first edit?
EDIT3:
Well..Seems like it's not a network issue but a .Net compact framework (usage?) issue...
The Url property of the Web Service implementation is simply ignored and the one in the Reference.cs is used in place.
If someone had some idea on how I could troubleshot this, I would really appreciate it.
That error means that you reached a server and the server said "no way". So you're either hitting the wrong server or the wrong port.
I find the telnet client is useful for testing stuff like this. From the command line, you can do:
telnet [servername] [port]
So something like:
telnet myServerName 8087
If it goes to a blank screen, then it connected successfully. If it does not connect, it'll tell you.
The telnet client is no longer installed by default in Windows 7+, so you'll have to install it. See here for instructions: https://technet.microsoft.com/en-ca/library/cc771275
If the connection does open, you could paste in an actual HTTP request to see what happens. A simple GET would look something like this:
GET /myServerApp/services/myService HTTP/1.1
Host: myServerName:8087
One reason for this error can be that the service binds to only a certain IP address. It could well be that the service only listens on the IP that is assigned to the host name, but not on the localhost IP (127.0.0.1).
For example:
If the host myServerName has the public IP 192.168.0.1, your service can choose to listen on all IPs assigned to the host (sometimes specifying 0.0.0.0), or it can specifically listen on 192.168.0.1 only. In that case you will not be able to connect through 127.0.0.1, because the service simply doesn't listen on that IP.
You can "use" this inverse of this feature to make a service accessible only to local clients, not on the public IP-Address, by listening on 127.0.0.1 only, but not on the public IP. This is sometimes used on Linux for example to make MySQL only accessible on the host itself.
I was starting to forget this post but I finally found the problem that was messing things up and it has nothing to do with programmation.
I was doing the calls while the device was connected to the computer via the 'Windows Mobile Device Center' allowing to access the device from Windows.
While connected, the host provided is ignored and all calls on the specified port are handled by the connected computer.
Disconnecting the device allows to communicate properly...

Azure connection failed because connected host has failed to respond ip:443

So I implemented an interface to communicate with a rest web service using the HttpClient class to make requests.
The code works perfectly locally, but when I deploy to Azure my application can't fire the request, it crashes on this line:
using (var response = await HttpClient.PostAsync(uri, content)) { ... }
// uri = https://api-rest.zenvia360.com.br/services/send-sms
The exact exception message is this:
A connection attempt failed because the connected party did not properly
respond after a period of time, or established connection failed because
connected host has failed to respond 200.203.125.26:443
The web service provider states that "if you use firewall or proxy you must add and exception for our IPs. For HTTPS, use port 443:
200.203.125.24
200.203.125.25
200.203.125.26 (the ip of the exception message)
200.203.125.27
200.203.125.28
200.203.125.29"
I looked everywhere in Azure looking for a firewall or something and I got nothing. Also this exception message is pretty cryptc. I tested the same code with another url (fired a post to www.gooogle.com) and it worked.
Any ideas?
The problem turned out to be on the web service side. The service I'm using blocks international requests by default. I asked them to whitelist the azure outbound IPs and now it works.

Windows service crashes with WebClient.DownloadData(url) on external server

I have a windows service that needs to get data from a web page.
try
{
WebClient client = new WebClient();
byte[] test = client.DownloadData(url);
Encoding latin1 = Encoding.GetEncoding("ISO-8859-1");
string s = latin1.GetString(test);
htmlDoc.LoadHtml(s);
}
catch (Exception ex)
{
log.Fatal(ex.Message);
}
This code works perfectly fine when I'm debugging, I then install the service on my external webserver (using installutil) but when I try to launch the service it crashes on this particular line.
For some reason the error is not getting logged, so I don't understand why my code is failing. I'm guessing the service shuts down before getting into the catch.
I have also tried using HtmlAgilityPack's HtmlWeb.Load(url) but it crashed the same (which is logical I guess^^).
I've tried adding firewall security rules on the server (authorize inbound and outbound traffic on port 80) with no success.
I'm all out of ideas, any help would be greatly appreciated :)
PS: I know very little about windows services, it's the first one I have ever made.
I found out what the problem was.
The IP of my webserver must be banned from the site I'm trying to access.
I tried accessing the url using IE on my server, and the page just timed out..
My service was unable to start simply because it was taking too much time to launch due to the timed out page. It was not actually crashing, hence no exception was getting caught.
Silly me, should have tried this before. Hope this can help someone.
Cheers

Categories