public string InfoString(bool verbose) { string status = isDone && response != null?response.status.ToString() : "---"; string message = isDone && response != null ? response.message : "Unknown"; double size = isDone && response != null && response.bytes != null ? response.bytes.Length : 0.0f; int order = 0; while (size >= 1024.0f && order + 1 < sizes.Length) { ++order; size /= 1024.0f; } string sizeString = String.Format("{0:0.##}{1}", size, sizes[order]); string result = uri.ToString() + " [ " + method.ToUpper() + " ] [ " + status + " " + message + " ] [ " + sizeString + " ] [ " + responseTime + "ms ]"; if (verbose && response != null) { result += "\n\nRequest Headers:\n\n" + String.Join("\n", GetHeaders().ToArray()); result += "\n\nResponse Headers:\n\n" + String.Join("\n", response.GetHeaders().ToArray()); if (response.Text != null) { result += "\n\nResponse Body:\n" + response.Text; } } return(result); }
public string InfoString(bool verbose) { string text = ((isDone && response != null) ? response.status.ToString() : "---"); string text2 = ((isDone && response != null) ? response.message : "Unknown"); double num = ((isDone && response != null && response.bytes != null) ? ((float)response.bytes.Length) : 0f); int num2 = 0; while (num >= 1024.0 && num2 + 1 < sizes.Length) { num2++; num /= 1024.0; } string text3 = $"{num:0.##}{sizes[num2]}"; string text4 = uri.ToString() + " [ " + method.ToUpper() + " ] [ " + text + " " + text2 + " ] [ " + text3 + " ] [ " + responseTime + "ms ]"; if (verbose && response != null) { text4 = text4 + "\n\nRequest Headers:\n\n" + string.Join("\n", GetHeaders().ToArray()); text4 = text4 + "\n\nResponse Headers:\n\n" + string.Join("\n", response.GetHeaders().ToArray()); if (response.Text != null) { text4 = text4 + "\n\nResponse Body:\n" + response.Text; } } return(text4); }
public void Send() { if (sent) { throw new InvalidOperationException("Request has already completed."); } sent = true; isDone = false; state = RequestState.Waiting; #if USE_GZIP if (acceptGzip) { SetHeader("Accept-Encoding", "gzip"); } #endif ThreadPool.QueueUserWorkItem(new WaitCallback(delegate(object t) { try { var retry = 0; while (++retry < maximumRedirects) { if (useCache) { string etag = ""; if (etags.TryGetValue(uri.AbsoluteUri, out etag)) { SetHeader("If-None-Match", etag); } } var hostHeader = uri.Host; if (uri.Port != 80 && uri.Port != 443) { hostHeader += ":" + uri.Port.ToString(); } SetHeader("Host", hostHeader); if (enableCookies && uri != null) { try { var c = cookies.GetCookieHeader(uri); if (c != null && c.Length > 0) { SetHeader("Cookie", c); } } catch (NullReferenceException) { //Some cookies make the .NET cookie class barf. MEGH again. //Debug.Log (".NET cannot parse this cookie: " + e.ToString ()); } catch (IndexOutOfRangeException) { //Another weird exception that comes through from the cookie class. } } ActiveConnection connection; try { //pull a connection from the pool (a new one is created if needed) connection = GetClient(uri.Host, uri.Port, uri.Scheme.ToLower() == "https"); } catch (Exception e) { Debug.Log(e); exception = e; response = null; break; } try { WriteToStream(connection.stream); } catch (IOException e) { Debug.Log(e); exception = new IOException("Server closed the connection:" + e.ToString()); response = null; break; } response = new Response(this); state = RequestState.Reading; try { response.ReadFromStream(connection.stream); } catch (IOException e) { Debug.Log(e); exception = new IOException("Server closed the connection:" + e.ToString()); response = null; break; } if (response != null) { if (enableCookies) { foreach (var i in response.GetHeaders("Set-Cookie")) { try { cookies.SetCookies(uri, i); } catch (System.Net.CookieException) { //Some cookies make the .NET cookie class barf. MEGH. } } } switch (response.status) { case 101: upgradedConnection = connection; retry = maximumRedirects; break; case 304: retry = maximumRedirects; break; case 307: case 302: case 301: uri = new Uri(response.GetHeader("Location")); if (OnRedirect != null) { OnRedirect(uri); retry = maximumRedirects; } break; default: retry = maximumRedirects; break; } //close the connection back if not upgraded. if (upgradedConnection == null) { lock (connectionPool) { var close = response.GetHeader("Connection").ToLower() == "close"; if (!close) { connectionPool.Add(connection); } else { connection.stream.Close(); } } } } } if (useCache && response != null) { string etag = response.GetHeader("etag"); if (etag.Length > 0) { etags [uri.AbsoluteUri] = etag; SaveEtags(); } } } catch (Exception e) { Debug.Log(e); exception = e; response = null; } state = RequestState.Done; isDone = true; })); }
public void Send () { if (sent) throw new InvalidOperationException ("Request has already completed."); sent = true; isDone = false; state = RequestState.Waiting; #if USE_GZIP if (acceptGzip) SetHeader ("Accept-Encoding", "gzip"); #endif ThreadPool.QueueUserWorkItem (new WaitCallback (delegate(object t) { try { var retry = 0; while (++retry < maximumRedirects) { if (useCache) { string etag = ""; if (etags.TryGetValue (uri.AbsoluteUri, out etag)) { SetHeader ("If-None-Match", etag); } } var hostHeader = uri.Host; if (uri.Port != 80 && uri.Port != 443) hostHeader += ":" + uri.Port.ToString (); SetHeader ("Host", hostHeader); if (enableCookies && uri != null) { try { var c = cookies.GetCookieHeader (uri); if (c != null && c.Length > 0) { SetHeader ("Cookie", c); } } catch (NullReferenceException) { //Some cookies make the .NET cookie class barf. MEGH again. //Debug.Log (".NET cannot parse this cookie: " + e.ToString ()); } catch (IndexOutOfRangeException) { //Another weird exception that comes through from the cookie class. } } ActiveConnection connection; try { //pull a connection from the pool (a new one is created if needed) connection = GetClient (uri.Host, uri.Port, uri.Scheme.ToLower () == "https"); } catch (Exception e) { Debug.Log (e); exception = e; response = null; break; } try { WriteToStream (connection.stream); } catch (IOException e) { Debug.Log (e); exception = new IOException ("Server closed the connection:" + e.ToString ()); response = null; break; } response = new Response (this); state = RequestState.Reading; try { response.ReadFromStream (connection.stream); } catch (IOException e) { Debug.Log (e); exception = new IOException ("Server closed the connection:" + e.ToString ()); response = null; break; } if (response != null) { if (enableCookies) { foreach (var i in response.GetHeaders("Set-Cookie")) { try { cookies.SetCookies (uri, i); } catch (System.Net.CookieException) { //Some cookies make the .NET cookie class barf. MEGH. } } } switch (response.status) { case 101: upgradedConnection = connection; retry = maximumRedirects; break; case 304: retry = maximumRedirects; break; case 307: case 302: case 301: uri = new Uri (response.GetHeader ("Location")); if (OnRedirect != null) { OnRedirect (uri); retry = maximumRedirects; } break; default: retry = maximumRedirects; break; } //close the connection back if not upgraded. if (upgradedConnection == null) { lock (connectionPool) { var close = response.GetHeader ("Connection").ToLower () == "close"; if (!close) { connectionPool.Add (connection); } else { connection.stream.Close (); } } } } } if (useCache && response != null) { string etag = response.GetHeader ("etag"); if (etag.Length > 0) { etags [uri.AbsoluteUri] = etag; SaveEtags (); } } } catch (Exception e) { Debug.Log (e); exception = e; response = null; } state = RequestState.Done; isDone = true; })); }