public string GetResponse() { result.RetryCount++; while (true) { BaseServiceItem.CheckIsTerminated(); try { return(InternalGetResponse()); } catch (WebException we) { //keep alive of google without proxy is break over 9 secs without notify //retry in this case if (we.Status != WebExceptionStatus.KeepAliveFailure) { throw; } } } }
string InternalGetResponse() { Uri realUrl = url; if (useGoogleCache && contentType == WebRequestContentType.UrlEncodedGet) { realUrl = new Uri("http://www.google.com/search?q=cache:" + url.ToString().Substring(7)); } //request WebRequest request = WebRequest.Create(realUrl); result.BytesSent += realUrl.OriginalString.Length; request.Proxy = networkSetting.Proxy; request.Timeout = networkSetting.Timeout; if (contentType != WebRequestContentType.UrlEncodedGet) { request.Method = "POST"; result.BytesSent += 4; } else { request.Method = "GET"; result.BytesSent += 3; } result.BytesSent += 12; //system data HttpWebRequest webRequest = request as HttpWebRequest; try { //avoid error in mono webRequest.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate; } catch (NotImplementedException) { } if (!string.IsNullOrEmpty(acceptLanguage)) { webRequest.Headers[HttpRequestHeader.AcceptLanguage] = acceptLanguage; } if (!string.IsNullOrEmpty(acceptCharset)) { webRequest.Headers[HttpRequestHeader.AcceptCharset] = acceptCharset; } //headers webRequest.UserAgent = "Mozilla/5.0, Translate.Net"; //webRequest.UserAgent = "Mozilla/5.0"; if (string.IsNullOrEmpty(referer)) { if (useGoogleCache) { webRequest.Referer = "http://www.google.com"; } else { webRequest.Referer = url.AbsoluteUri; } } else { webRequest.Referer = referer; } request.Headers.Add(HttpRequestHeader.KeepAlive, "300"); request.Credentials = CredentialCache.DefaultCredentials; if (cookieContainer != null) { webRequest.CookieContainer = cookieContainer; } if (contentType == WebRequestContentType.UrlEncoded) { request.ContentType = "application/x-www-form-urlencoded"; } else if (contentType == WebRequestContentType.XmlRpc) { request.ContentType = "text/xml"; } else if (contentType == WebRequestContentType.Multipart) { request.ContentType = "multipart/form-data; boundary=" + this.multiPartBoundary; if (!alreadyGenerated) { postData.Write(Encoding.UTF8.GetBytes("--" + this.multiPartBoundary + "\r\n")); } } alreadyGenerated = true; if (contentType != WebRequestContentType.UrlEncodedGet) { Stream requestStream = request.GetRequestStream(); if (contentType != WebRequestContentType.XmlRpc) { postData.Flush(); } else { postXmlData.Flush(); } if (gZipRequest) { request.Headers.Add("Content-Encoding", "gzip"); GZipStream gzipStream = new GZipStream(requestStream, CompressionMode.Compress); postStream.WriteTo(gzipStream); gzipStream.Close(); } else { postStream.WriteTo(requestStream); } requestStream.Close(); } WebResponse response = request.GetResponse(); BaseServiceItem.CheckIsTerminated(); result.BytesSent += request.Headers.ToByteArray().Length; if (request.ContentLength != -1) { result.BytesSent += request.ContentLength; } result.BytesReceived += response.Headers.ToByteArray().Length; Stream responseStream = response.GetResponseStream(); if (streamConvertor != null) { responseStream = streamConvertor.ConvertStream(responseStream); } StreamReader reader = new StreamReader(responseStream, encoding); string resultStr = ""; try { StringBuilder sb = new StringBuilder(); char[] buffer = new char[4096]; int readed_cnt = reader.Read(buffer, 0, 4096); do { sb.Append(buffer, 0, readed_cnt); readed_cnt = reader.Read(buffer, 0, 4096); BaseServiceItem.CheckIsTerminated(); }while(readed_cnt > 0); //string resultStr = reader.ReadToEnd (); resultStr = sb.ToString(); if (response.ContentLength != -1) { result.BytesReceived += response.ContentLength; } else if (!string.IsNullOrEmpty(response.Headers[HttpResponseHeader.ContentLength])) { result.BytesReceived += int.Parse(response.Headers[HttpResponseHeader.ContentLength], CultureInfo.InvariantCulture); } else { result.BytesReceived += resultStr.Length; } result.BytesReceived += 21; //system data } finally { reader.Close(); responseStream.Close(); response.Close(); } return(resultStr); }