Reputation: 121
I'm developing program for WP8, that retrieves data from JSON. Connection is secured (url start with https). Sometimes it works fine, but sometimes I start receiving exceptions
System.Net.WebException: The remote server returned an error: NotFound.
And this exceptions starts showing for all requests to all https URLs. For http URLs everything goes fine. URL is OK, it is opened in IE on emulator.
I think, there could be problems with certificate, but why it works sometimes?
public static void SendRequest(string requestUrl, Action<Stream, Exception> callback)
{
var targetUri = new Uri(requestUrl);
var request = (HttpWebRequest)WebRequest.Create(targetUri);
request.Method = "POST";
request.BeginGetResponse(ar => ProcessResponse(ar, callback), request);
}
public static void ProcessResponse(IAsyncResult callbackResult, Action<Stream, Exception> callback)
{
try
{
var myRequest = (HttpWebRequest)callbackResult.AsyncState;
var myResponse = (HttpWebResponse)myRequest.EndGetResponse(callbackResult);
callback(myResponse.GetResponseStream(), null);
myResponse.Close();
}
catch (Exception e)
{
callback(Stream.Null, e);
Debug.WriteLine("Error in RequestHelper.ProcessResponse\nErrorMessage - " + e.Message);
}
}
Updated: The issue is definitely in SSL certificate. I've got a message, when I tried to open web page on WebBrowser control - "We're having trouble with this site's security certificate.". I've clicked "Continue to website", but nothing changed. The page still can be opened by IE on emulator. I've tried to install certificate (Made export from site, and downloaded it with IE. I've got the message, that certificate was successfully added. But I still have error message on WebBrowser control. Is there any way to install certificate? Or not check for it's validity?
Upvotes: 2
Views: 2419
Reputation: 11
Self signed certificate is working fine but you need right Common Name (CN) in your certificate. CN must be same as your server domain
and than you will need to import cert. to your phone
Upvotes: 1
Reputation: 788
I have met same problem in two applications that are communicated with a sever by https
and I haven't found how to fix it. But I hacked it. When I get a WebException: Remote server not found
I will check a StatusCode
of a Response
and a Status
of the web exception and if the Status
is not RequestCancelled
(you get such status in fast app switching) I repeat the request. It looks something like this:
var httpStatusCode = ((HttpWebResponse) webException.Response).StatusCode;
if (httpStatusCode == HttpStatusCode.NotFound ||
httpStatusCode == HttpStatusCode.GatewayTimeout ||
httpStatusCode == HttpStatusCode.InternalServerError)
{
if (webException.Status == WebExceptionStatus.UnknownError && !configuration.IsResending)
{
configuration.IsResending = true;
ResendRequest(configuration, successAction);
return;
}
configuration.IsResending = false;
throw new ServerTemporaryUnavailabeException();
}
And the second time execution of the request doesn't return WebException: Not found
.
Upvotes: 0