Reputation: 17915
How is this exception possible?
I'm calling google service as shown below. My logs identified that I get Timeout exception on line var requestStream = request.GetRequestStream();
I didn't even place it inside catch
block because I thought reading stream is getting it from string (which is pretty small). So, how can I get timeout on this? Is it possible that google server takes time and reading happens slow and I'm getting this?
// Google had issues with SSL certificates. We know address is good, so just kill validation
// and trust any certificate it might have..
ServicePointManager.ServerCertificateValidationCallback = (sender, certificate, chain, errors) => true;
// Prepare HTTPS request. 5 seconds timeout should be good for google.
const string Uri = "https://android.apis.google.com/c2dm/send";
var request = (HttpWebRequest)WebRequest.Create(Uri);
request.Headers.Add("Authorization", "GoogleLogin auth=" + this.SecurityToken);
request.ContentType = "application/x-www-form-urlencoded";
request.Method = "POST";
request.Timeout = 5000;
// build the post string
var postString = new StringBuilder();
postString.AppendFormat("registration_id={0}", recipientId);
postString.AppendFormat("&data.payload={0}", message);
postString.AppendFormat("&collapse_key={0}", collapseKey);
// write the post-string as a byte array
var requestData = Encoding.ASCII.GetBytes(postString.ToString());
request.ContentLength = requestData.Length;
var requestStream = request.GetRequestStream();
requestStream.Write(requestData, 0, requestData.Length);
requestStream.Close();
// Do the actual request and read the response stream
try
{
var response = request.GetResponse();
var responseString = GetResponseString(response);
response.Close();
return responseString.Contains("id=")
? SendStatus.Ok
: GetSendStatusFromResponse(responseString);
}
catch (WebException ex)
{
var webResponse = (HttpWebResponse)ex.Response;
if (webResponse != null)
{
Upvotes: 0
Views: 1075
Reputation: 61502
GetRequestStream
opens a connection and returns an already-connected stream. Anything you write to the stream goes out on the wire, straight away. It's not buffered up in its entirety first (though some buffering is almost certainly involved).
So it makes sense that it can time out; it just means that a TCP connection could not be established, because that's the point when the connection is set up.
Upvotes: 1