private void PrepareConnection(HttpRequestInfo requestInfo) { WebProxy proxy = null; if (NetworkSettings != null) { if (NetworkSettings.WebProxy != null) { proxy = (NetworkSettings.WebProxy as WebProxy); if (proxy != null) { _connection = new HttpClientConnection(proxy.Address.Host, proxy.Address.Port, false); } } } if (proxy != null) { _connection = new HttpClientConnection(proxy.Address.Host, proxy.Address.Port, false); } else { _connection = new HttpClientConnection(requestInfo.Host, requestInfo.Port, requestInfo.IsSecure); } }
/// <summary> /// Creates a request with a predefined connection /// </summary> /// <param name="connection"></param> public HttpClientRequest(HttpClientConnection connection) { _connection = connection; }
/// <summary> /// Sends the specified request to the server /// (Proxy not supported) /// </summary> /// <param name="parsedRequest"></param> /// <param name="host"></param> /// <param name="port"></param> /// <param name="https"></param> public void SendRequest(HttpRequestInfo parsedRequest, string host, int port, bool https) { _requestCompleteEvent.Reset(); _dataBuilder = new ByteArrayBuilder(); if (_connection == null) { _connection = new HttpClientConnection(host, port, https); } try { //connect if (_connection.Connect()) { bool isProxy = _connection.Host != host; //add connection closed header only this is supported at the moment parsedRequest.Headers["Connection"] = "close"; if (isProxy) { parsedRequest.Headers["Proxy-Connection"] = "close"; } // Turn off accepting of gzip/deflate //parsedRequest.Headers.Remove("Accept-Encoding"); //calculate the content length if (parsedRequest.ContentData != null) { parsedRequest.Headers["Content-Length"] = parsedRequest.ContentData.Length.ToString(); } parsedRequest.Host = host; parsedRequest.Port = port; parsedRequest.IsSecure = https; if (isProxy && https) { //send a connect message to the proxy SendConnect(host, port); } byte[] reqBytes = Constants.DefaultEncoding.GetBytes(parsedRequest.ToString(isProxy && !https)); //write to the stream _connection.Stream.Write(reqBytes, 0, reqBytes.Length); //start reading _buffer = new byte[MAX_BUFFER_SIZE]; _connection.Stream.BeginRead(_buffer, 0, MAX_BUFFER_SIZE, new AsyncCallback(ReadResponse), _connection.Stream); } else { throw new Exception("Cannot connect to server"); } } catch (Exception ex) { SdkSettings.Instance.Logger.Log(TraceLevel.Error, "HttpClient error sending request {0}", ex.Message); //notify the caller that the request was completed with an error if (RequestComplete != null) { RequestComplete.Invoke( new HttpClientRequestCompleteEventArgs()); RequestCompleteEvent.Set(); } _connection.Close(); } }