System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a send

314 views
Skip to first unread message

ripu shah

unread,
Aug 28, 2017, 7:34:57 PM8/28/17
to Google Maps JavaScript API v3
 I have used to get latitude & longitude  by using google Geo-coding API  but while send a request i got this error can you help me to how resolve this issue?

code : 
   string fullAddress = string.Empty;
        if (!string.IsNullOrEmpty(address) || zipCode != null)
        {
            fullAddress = string.Format("{0} {1}", address.Trim().Replace(' ', '+'), zipCode);
        }
        string url = "https://maps.googleapis.com/maps/api/geocode/json?address={0}&sensor=true&key={1}";         //Map.GetMapGeoLocationV2(fullAddress);

        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
        request.KeepAlive = false;
        request.ProtocolVersion = HttpVersion.Version10;
        request.Credentials = System.Net.CredentialCache.DefaultCredentials;
        request.UseDefaultCredentials = true;
        request.ConnectionGroupName = Guid.NewGuid().ToString();
       // ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;

    
        request.Method = "GET";
       
        // var response = request.GetResponse();
        try
        {
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            System.Threading.Thread.Sleep(1000);
            using (var stream = response.GetResponseStream())
            {
                using (var reader = new StreamReader(stream))
                {
                    var result = Newtonsoft.Json.Linq.JObject.Parse(reader.ReadToEnd());
                    outstatus = result.SelectToken("status").ToString() != null ? result.SelectToken("status").ToString() : string.Empty;
                    if (outstatus == "OK")
                    {
                        latitude = result.SelectToken("results[0].geometry.location.lat").ToString() != null ? result.SelectToken("results[0].geometry.location.lat").ToString() : string.Empty;
                        longitude = result.SelectToken("results[0].geometry.location.lng").ToString() != null ? result.SelectToken("results[0].geometry.location.lng").ToString() : string.Empty;
                        var addresscomponentsCount = result.SelectToken("results[0].address_components").Count();
                        for (int i = 0; i < addresscomponentsCount; i++)
                        {
                            var addresscomponentsTypesCount = result.SelectToken("results[0]." + string.Format("address_components[" + "{0}" + "]", i) + ".types").Count();

                            for (int j = 0; j < addresscomponentsTypesCount; j++)
                            {
                                var type = result.SelectToken("results[0]." + string.Format("address_components[" + "{0}" + "]", i) + string.Format(".types[" + "{0}" + "]", j)).ToString();
                                if (type == "administrative_area_level_1")
                                {
                                    state = result.SelectToken("results[0]." + string.Format("address_components[" + "{0}" + "]", i) + ".short_name").ToString();
                                }
                                if (type == "postal_code")
                                {
                                    outZipCode = result.SelectToken("results[0]." + string.Format("address_components[" + "{0}" + "]", i) + ".short_name").ToString();
                                }
                            }
                        }
                        response.Close();
                    }
                    reader.Close();
                }
                stream.Close();
                
            }

        }

error log: 

System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a send. ---> System.IO.IOException: Received an unexpected EOF or 0 bytes from the transport stream. at System.Net.FixedSizeReader.ReadPacket(Byte[] buffer, Int32 offset, Int32 count) at System.Net.Security.SslState.StartReadFrame(Byte[] buffer, Int32 readBytes, AsyncProtocolRequest asyncRequest) at System.Net.Security.SslState.StartReceiveBlob(Byte[] buffer, AsyncProtocolRequest asyncRequest) at System.Net.Security.SslState.CheckCompletionBeforeNextReceive(ProtocolToken message, AsyncProtocolRequest asyncRequest) at System.Net.Security.SslState.StartSendBlob(Byte[] incoming, Int32 count, AsyncProtocolRequest asyncRequest) at System.Net.Security.SslState.ForceAuthentication(Boolean receiveFirst, Byte[] buffer, AsyncProtocolRequest asyncRequest) at System.Net.Security.SslState.ProcessAuthentication(LazyAsyncResult lazyResult) at System.Net.TlsStream.CallProcessAuthentication(Object state) at System.Threading.ExecutionContext.runTryCode(Object userData) at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData) at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Net.TlsStream.ProcessAuthentication(LazyAsyncResult result) at System.Net.TlsStream.Write(Byte[] buffer, Int32 offset, Int32 size) at System.Net.PooledStream.Write(Byte[] buffer, Int32 offset, Int32 size) at System.Net.ConnectStream.WriteHeaders(Boolean async) --- End of inner exception stack trace --- at System.Net.HttpWebRequest.GetResponse() at WebRequestCreate
Reply all
Reply to author
Forward
0 new messages