`Server Not Found` error in webclient Windows Phone - c#

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.

Related

C# HttpListener (netsh) problems with SelfSigned Certificate

I have some trouble with my HttpListener. I already searched and read some dicussions.
For example: Httplistener with HTTPS support
Firstly I try to describe my scenario:
I want to create a HTTP-Listener with SSL/HTTPS Support. Thats the main target.
I used OpenSSL to create my own CA and I created my own server cert.
Now I have:
myCa.key
myCa.pem
myCa.srl
myServer.key
myServer.csr
myServer.crt
I installed the myCa.pem and the myServer.crt certificate to my local computer. I moved the CA in the trusted store and the server certificate in "own certificates"
Then I took the fingerprint (certHash) of my server certificate.
I created the netsh entry with admin-rights
netsh http add sslcert ipport=0.0.0.0:9649 appid= '{0a5ce-569a-4dc6-8ed7-9ef91241dec3}' certhash=4F556BDC3F97B31D555266DA74F573777FCCAA55
My C# implementation is relativly simple:
this.Listener = new HttpListener();
this.Listener.Prefixes.Add("https://*:9649");
this.Listener.Start();
this.Listener.BeginGetContext(new AsyncCallback(ProcessClient), this.Listener);
//Process an incoming connection
private void ProcessClient(IAsyncResult result)
{
var listener = (HttpListener)result.AsyncState;
var clientContext = listener.EndGetContext(result);
}
When I implemented SSL in my TcpStack I used a SSL-Stream and there I can validate a certificate with a ValidationCallback. Im not sure if this is possible here. I tried
ServicePointManager.ServerCertificateValidationCallback += ValidateCert; But I never hit my breakpoint there.
Now to the problems:
When I try to connect with my own HttpClient (.NET HttpClient Class) I get always a RemoteNameMismatch Error on the SSL-Layer. I dont hit the breakpoint in the ProcessClient method. I tried without specific certificate (auto detection) and I tried also to advise the same certificate (with the same hash) to the client. In both cases I got the same error. I dont understand why I get any erros when I use the same certificate on the client and the server side. I always thought the netsh will compare the certhashes.
When I try a connect with Postman I hit the ProcessClient function. But Postman gets an error that he cant check the certificate. But I think the problem is that my certificate isnt a official certifcate . But the data exchange is working.
Another point is: I want to roll out my app also in containers with a unix os. .NET60 is designed for crossplatform. But whats the unix pendant to netsh? Is it possible to run my listener with https on unix? How works the mapping here between app and certificate?
Maybe I have to change my technology? Alternative to HttpListener? Mainly I dont want to use thridparty stuff.
UPDATE Solution:
See my answer below
Thanks for reading and for help.
Greetings
Like the guys said in the in comments. The FDQN was the problem. In easy words: I created my own CA and then I created a server cert signing request against the CA. Inside the server cert the CN is matching to my DNS of my personal computer. The connection with my HTTP-Listener is working now. Thank you for your help!

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.

Microsoft's url for the Exchange powershell schema is not valid anymore

I am trying to create a remote connection to an exchange Powershell hosted on IIS 8.5 - Windows Server 2012 R2.
Here's my code :
var connInfo = new WSManConnectionInfo(
new Uri("https://xxx/PowerShell"),
"http://schemas.microsoft.com/powershell/Microsoft.Exchange",
credentials);
It look like the Microsoft's Schemas url is not valid anymore. If I navigate to http://schemas.microsoft.com/powershell/Microsoft.Exchange, I have this :
The resource you are looking for has been removed, had its name changed, or is temporarily unavailable.
And when I try to open my connection, I have this error :
Connecting to remote server failed with the following error message : The WinRM client received an HTTP bad request status (400), but the remote service did not include any other information about the cause of the failure.
Is it possible the url changed ? I trid to find a topic about it, but I found no trace of a change...
The schema uri doesn't have to be 'valid' in the sense you don't have to be able to navigate to it. I know I'm not explaining it properly but it's more a reference than a valid location.
The error you're receiving is actually referring to the uri you have for your Exchange Server. If you aren't connecting to an Exchange Server the connection will error.

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

c# programmatically reading emails from the Exchange server

When you search on web you will find very easy answers for "How to read emails programmatically"... Al the websites are explaining most of the same like this page.
http://omegacoder.com/?p=454
// depends from Exchange server version
service.Credentials = new NetworkCredential("MDR", "password", "zzz");
service.AutodiscoverUrl("mdr#zzz.be");
object o = service.FindItems(WellKnownFolderName.Inbox, new ItemView(10));
FindItemsResults<Item> findResults = service.FindItems(WellKnownFolderName.Inbox, new ItemView(10));
foreach (Item item in findResults.Items)
{
Console.WriteLine(item.Subject);
}
it fails when it executes the autodiscoverURL line. The error says "The Autodiscover service couldn't be located."
So I googled further and found this site from Microsoft https://www.testexchangeconnectivity.com/#&&/wEXAQUBcwUBME93h2+JjI0+MV2gTqcRL0g43z9m Here you can test your mail server....
When I pass the parameters I get the error below....
But I still don't understand what the problem is? Do I need to add a record to DNS ? Can someone help?
Attempting to test potential Autodiscover URL https://autodiscover.zzz.be/AutoDiscover/AutoDiscover.xml
Testing of this potential Autodiscover URL failed.
Test Steps
Attempting to resolve the host name autodiscover.ncb.be in DNS.
The host name resolved successfully.
Additional Details
IP addresses returned: 213.246.192.205
Testing TCP port 443 on host autodiscover.ncb.be to ensure it's listening and open.
The specified port is either blocked, not listening, or not producing the expected response.
Tell me more about this issue and how to resolve it
Additional Details
A network error occurred while communicating with the remote host.
Exception details:
Message: No connection could be made because the target machine actively refused it 213.246.192.205:443
Type: System.Net.Sockets.SocketException
Stack trace:
at System.Net.Sockets.TcpClient.Connect(String hostname, Int32 port)
at Microsoft.Exchange.Tools.ExRca.Tests.TcpPortTest.PerformTestReally()
Attempting to contact the Autodiscover service using the HTTP redirect method.
The attempt to contact Autodiscover using the HTTP Redirect method failed.
Test Steps
Attempting to resolve the host name autodiscover.zzz.be in DNS.
The host name resolved successfully.
Additional Details
IP addresses returned: 213.246.192.205
Testing TCP port 80 on host autodiscover.zzz.be to ensure it's listening and open.
The port was opened successfully.
ExRCA is checking the host autodiscover.zzz.be for an HTTP redirect to the Autodiscover service.
ExRCA failed to get an HTTP redirect response for Autodiscover.
Additional Details
A Web exception occurred because an HTTP 404 - NotFound response was received from IIS7.
Attempting to contact the Autodiscover service using the DNS SRV redirect method.
ExRCA failed to contact the Autodiscover service using the DNS SRV redirect method.
Test Steps
Attempting to locate SRV record _autodiscover._tcp.ncb.be in DNS.
The Autodiscover SRV record wasn't found in DNS.
Tell me more about this issue and how to resolve it
You don't necessarily need to use the autodiscovery if you already know the address of your exchange server. Try the following instead (for more info, look here:
service.Url = new Uri("https://hostname/EWS/Exchange.asmx");
Replace "hostname" with the hostname for your exchange server.
I hope you should have the solution by this time now. This is just to help anyone bumped on this post.
I found the solution on one of the technet article, I twick to suite me, and is working fine for me.
Just replace the line in your code with following:
service.AutodiscoverUrl("user#yourdomain.com",
delegate
{
return true;
});
I had some other issues but not related to this bit though.
Happy Coding,
Sanjay.
I had the same issue with AutoDiscover. It's not necessary, you can specify your URL like
Uri myUri = new Uri("https://Hostname/ews/exchange.asmx");
userData.AutodiscoverUrl = myUri;
service.Url = myUri;
As the hostname you can put the Server IP address like 192.168.100.10
Alternatively, to find what your Exchange server hostname is (in in fact the whole url to use) if you are using Outlook, go to your computer start bar, where the Date and time is showing, you will find the Outlook icon, hold Ctrl + right click on the outlook icon and click “Test Email Auto Configuration”
Check the "Use AutoDiscover" checkbox. Enter an email address hosted on that Exchange Server along with its password and you will recieve a bunch of url's. Use the 1 that says "Availability Service URL"
Consider that the credentials being passed need to have permission to the given exchange mailbox / server. In my case using a different set of credentials that are properly permissioned works but not for a service account which I'm trying to get to work.
Once I discover what exactly the account needs to be permissioned for I will update it here.
Update: My issue was the service account was from a domain different than the domain on which the exchange 2007 instance is running, even though there is a trust relationship between the two. I found this is a documented known issue in Exchange 2007 in how it looks up accounts in its forest. In the end had to create an identical service account (name/pass) on the domain on which the exchange server is sitting and specify username as {exchange_domain}{service_account_name}. The windows service that calls EWS runs as {original_domain}{service_account_name}.
For reference, the exception was:
Microsoft.Exchange.WebServices.Data.ServiceResponseException:
Failed to get valid Active Directory information for the calling account. Confirm that it is a valid Active Directory account.

Categories