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

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

Related

The remote name could not be resolved http://schemas.microsoft.com/cdo/configuration/smtpauthenticate

Full error
System.Exception: Error sending email (0 bytes): Mailbox unavailable.
The server response was: 5.7.1 Client does not have permissions to
send as this sender ---> System.Net.Mail.SmtpException: Mailbox
unavailable. The server response was: 5.7.1 Client does not have
permissions to send as this sender
I inherited a project five years ago, and it is at least six years old. The project contains a couple of web applications and a few desktop apps & services.
The web application sends email pretty regularly, and it has been running without much in the way of problems until recently.
When we started getting this error, I tried a few things on my end. I created a small test web app and deployed it on a separate server. I used the same mail server, address, and credentials. It works fine. I also used dbmail on the same server the problem app is located, again using the same mail address. It works also.
I've read maybe six or seven questions & answers on the subject here and elsewhere, to no avail. It seems pretty clear that this is the mail server returning this error. But why would SQL Server's dbmail and the test app I made send if that were the case?
Here is the connection information with a few changes for the sake of anonymity.
static void SendEmail(MailMessage msg)
{
msg.Headers["http://schemas.microsoft.com/cdo/configuration/smtpauthenticate"] = "2";
using (var c = new SmtpClient
{
Host = "mailserver.address.com",
Port = 587,
UseDefaultCredentials = false,
Credentials = new System.Net.NetworkCredential("UserName", "password", "address.com"),
EnableSsl = false,
DeliveryMethod = SmtpDeliveryMethod.Network
})
{
try
{
c.Send(msg);
}
catch (Exception ex)
{
var size = msg.Attachments.Sum(a => a.ContentStream.Length);
throw new Exception(string.Format("Error sending email ({0} bytes): {1}", size, ex.Message), ex);
}
}
}
What has greatly exacerbated the problem is that the source code I have for roughly half of these apps is clearly not the identical of the code deployed. They had zero version control when I first started years ago. So, I can't easily tweak around in the code and redeploy because it will break the entire application. I would have to do a full-blown overhaul (and actually, I am, but it's not going to be ready anytime in the immediate future).
Any advice is greatly appreciated.
I've found the cause.
The port wasn't added in my test. When added, it causes the error.
Why would this break on a certain date this month?
This is a mail server issue. A sysadmin is the only person that can fix this, and I was able to get them to resolve. Thanks.

Unable to resolve hostname over vpn

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!

`Server Not Found` error in webclient Windows Phone

I am developing a Windows Phone application in which I have to make a call to a webservice which is https and is running on a computer which I access through a public IP. Now my webclient is not able to get the data from that service. It says the error was a System.Reflection.TargetInvocation exception and the inner exception tells that the remote server was not found. I thought it might be the certification issue but I have a .pem cert file which I install on my emulator but the error still persist. Can anybody tell what can be the root cause of this issue?
These problem arises due to these major issue,
1) You should have an active Internet connection to achieve this. Check your emulator whether it has proper internet connection.
2) Your headers should be in proper format. Try going through this link. It might help you in passing the headers in correct format. If you try to access the site which is secured with username and password, you need to pass those credentials. You can get these informations in above link.
Try with this sample json link whether you can achieve your target by getting the IP address. You can also try with HTTPWebRequest and HTTPWebResponse.

Cannot access wcf service hosted on iis from php with soapclient

This has been really painful for the last few hours and I just have no more ideas what to do.
Tried many solutions that I found on here without any luck...
I have an iis server that is hosting a wcf web service. Simple stuff.
I can connect to this (example: http://mydomain.com/Service.svc?wsdl) from the iis server and all other computers without any problems.
So I went on to make a php client on my computer - (using xampp via localhost) - (not on the iis server)
$wcfClient = new SoapClient('http://mydomain.com/Service.svc?wsdl');
$args = //input data
$response = $wcfClient->GetData($args);
print_r($response->GetDataResult->Post);
This works perfect. No problems at all. It returns all the data... Then I went on to upload this to my hosting account. This is the error I keep getting.
Fatal error: Uncaught SoapFault exception: [WSDL] SOAP-ERROR: Parsing WSDL: Couldn't load from ....
I have no more ideas. Disabled firewall, changed web.config a million times and still no luck. I tried the file_get_contents method to see what's up. I keep getting:
failed to open stream: Connection refused
I just fixed this by changing the port to 80 on the iis server...

Web Reference Help: No connection could be made because the target machine actively refused it

Ok, so I'm learning about ASP.NET by making a simple program to read in a user's gmail credentials and construct an email from their input into the Console, but I'm getting a new error that I was not get yesterday, and I have not changed ANY code. The error is on line 41, so I have provided that (the rest of the code is just reading input into Strings) I have disabled my firewall to no avail, and have also tried "http://localhost/Service.asmx", which doesn't work either. I've also tried restarting IIS.
I'm completely new to this subject, but the error suggests, to me, that the program is communicating with the server but is being denied by a blocked port, but I have disabled my firewall and the error persists. Any ideas? Thanks in advance!
/*40*/ Communication_Service.Service CommServiceProxy = new Communication_Service.Service();
/*41*/ Result = CommServiceProxy.SendMail(GmailAccount, Password, To, Subject, Body);
There was an error dowloading http://localhost:4932/Service.asmx. Unable to connect to the remove server. No connection could be made because the target machine actively refused it 127.0.0.1:4492
Sounds like you are running cassini (development server). The application at http://localhost:4932/ needs to be started. You probably have multiple sites and need multiple start up projects. Right click your solution and click set start up project, pick multiple, and select all your websites. On VS goto debug, run without debugging and they should all start.

Categories