Inheritance: HttpOutgoingRequestResponse
Esempio n. 1
0
        public HttpClientResponse GetResponse(String url)
        {
            HttpClientRequest lRequest = new HttpClientRequest();

            lRequest.Url.Parse(url);
            lRequest.Header.RequestType = "GET";
            lRequest.Header.SetHeaderValue("Accept", Accept);
            lRequest.Header.SetHeaderValue("User-Agent", UserAgent);
            lRequest.KeepAlive = KeepAlive;

            return(this.Dispatch(lRequest));
        }
Esempio n. 2
0
        public String Post(String url, Stream content)
        {
            HttpClientRequest lRequest = new HttpClientRequest();

            lRequest.Url.Parse(url);
            lRequest.RequestType = RequestType.Post;
            lRequest.Header.SetHeaderValue("Accept", Accept);
            lRequest.Header.SetHeaderValue("User-Agent", UserAgent);
            lRequest.KeepAlive     = KeepAlive;
            lRequest.ContentStream = content;

            using (HttpClientResponse response = this.Dispatch(lRequest))
                return(response.ContentString);
        }
Esempio n. 3
0
        public HttpClientResponse Dispatch(HttpClientRequest request)
        {
            HttpClientResponse lResponse = this.TryDispatch(request);

            if (lResponse.Code >= 400)
            {
                if (lResponse.HasContentLength)
                {
                    throw new HttpException(lResponse.ContentString, lResponse);
                }

                throw new HttpException(lResponse.Header.ToString(), lResponse);
            }

            return(lResponse);
        }
Esempio n. 4
0
        public HttpClientResponse TryDispatch(HttpClientRequest request)
        {
            HttpClient.SetAuthorizationHeader(request.Header, "Authorization", this.UserName, this.Password);

            String  lHostname      = request.Url.Hostname;
            Int32   lPort          = request.Url.Port;
            Boolean lSslConnection = String.Equals(request.Url.Protocol, "https", StringComparison.OrdinalIgnoreCase);

            // Settings for connection thru Http Proxy
            // Note that Request should think that it uses direct connection when SSL is enabled because
            // proxy server tunnels SSL data AS IS, without adjusting its HTTP headers
            request.UseProxy = this.ProxySettings.UseProxy && !lSslConnection;
            if (this.ProxySettings.UseProxy)
            {
                lHostname = this.ProxySettings.ProxyHost;
                lPort     = this.ProxySettings.ProxyPort;

                HttpClient.SetAuthorizationHeader(request.Header, "Proxy-Authorization", this.ProxySettings.UserName, this.ProxySettings.Password);
            }

            Connection lConnection = this.GetHttpConnection(lSslConnection, request.Url.Hostname, request.Url.Port, lHostname, lPort);

            try
            {
                request.WriteHeaderToConnection(lConnection);
            }
            catch (ConnectionClosedException)
            {
                lConnection = this.GetNewHttpConnection(lHostname, lPort);
                request.WriteHeaderToConnection(lConnection);
            }
            catch (System.Net.Sockets.SocketException)
            {
                lConnection = this.GetNewHttpConnection(lHostname, lPort);
                request.WriteHeaderToConnection(lConnection);
            }

            request.WriteBodyToConnection(lConnection);

            lConnection.Timeout        = Timeout;
            lConnection.TimeoutEnabled = TimeoutEnabled;

            HttpClientResponse lResponse;

            do
            {
                HttpHeaders lHeaders = HttpHeaders.Create(lConnection);
                if (lHeaders == null)
                {
                    throw new ConnectionClosedException();
                }
                lResponse = new HttpClientResponse(lConnection, lHeaders);
            }while (lResponse.Header.ResponseCode == "100"); // 100 CONTINUE means useless response.

            if (!lResponse.KeepAlive)
            {
                this.fConnectionUrl = null;
                this.fConnection    = null;
            }

            if (lResponse.Code == 407)
            {
                throw new HttpException("Proxy authorization failed", lResponse);
            }

            return(lResponse);
        }
Esempio n. 5
0
		public HttpClientResponse Dispatch(HttpClientRequest request)
		{
			HttpClientResponse lResponse = this.TryDispatch(request);

			if ((Int32)lResponse.HttpCode >= 400)
			{
				if (lResponse.HasContentLength)
					throw new HttpException(lResponse.ContentString, lResponse);

				throw new HttpException(lResponse.Header.ToString(), lResponse);
			}

			return lResponse;
		}
Esempio n. 6
0
		public HttpClientResponse TryDispatch(HttpClientRequest request)
		{
			HttpClient.SetAuthorizationHeader(request.Header, "Authorization", this.UserName, this.Password);

			String lHostname = request.Url.Hostname;
			Int32 lPort = request.Url.Port;
			Boolean lSslConnection = String.Equals(request.Url.Protocol, "https", StringComparison.OrdinalIgnoreCase);

			// Settings for connection thru Http Proxy
			// Note that Request should think that it uses direct connection when SSL is enabled because
			// proxy server tunnels SSL data AS IS, without adjusting its HTTP headers
			request.UseProxy = this.ProxySettings.UseProxy && !lSslConnection;
			if (this.ProxySettings.UseProxy)
			{
				lHostname = this.ProxySettings.ProxyHost;
				lPort = this.ProxySettings.ProxyPort;

				HttpClient.SetAuthorizationHeader(request.Header, "Proxy-Authorization", this.ProxySettings.UserName, this.ProxySettings.Password);
			}

			Connection lConnection = this.GetHttpConnection(lSslConnection, request.Url.Hostname, request.Url.Port, lHostname, lPort);

			try
			{
				request.WriteHeaderToConnection(lConnection);
			}
			catch (ObjectDisposedException)
			{
				lConnection = this.GetNewHttpConnection(lHostname, lPort);
				request.WriteHeaderToConnection(lConnection);
			}
			catch (ConnectionClosedException)
			{
				lConnection = this.GetNewHttpConnection(lHostname, lPort);
				request.WriteHeaderToConnection(lConnection);
			}
			catch (System.Net.Sockets.SocketException)
			{
				lConnection = this.GetNewHttpConnection(lHostname, lPort);
				request.WriteHeaderToConnection(lConnection);
			}

			request.WriteBodyToConnection(lConnection);


			HttpClientResponse lResponse;
			do
			{
				HttpHeaders lHeaders = HttpHeaders.Create(lConnection);
				if (lHeaders == null)
					throw new ConnectionClosedException();
				lResponse = new HttpClientResponse(lConnection, lHeaders);
			}
			while (lResponse.Header.HttpCode == HttpStatusCode.Continue); // 100 CONTINUE means useless response.

			if (!lResponse.KeepAlive)
			{
				this.fConnectionUrl = null;
				this.fConnection = null;
			}

			if (lResponse.HttpCode == HttpStatusCode.ProxyAuthenticationRequired)
				throw new HttpException("Proxy authorization failed", lResponse);

			return lResponse;
		}
Esempio n. 7
0
		public String Post(String url, Stream content)
		{
			HttpClientRequest lRequest = new HttpClientRequest();
			lRequest.Url.Parse(url);
			lRequest.RequestType = RequestType.Post;
			lRequest.Header.SetHeaderValue("Accept", Accept);
			lRequest.Header.SetHeaderValue("User-Agent", UserAgent);
			lRequest.KeepAlive = KeepAlive;
			lRequest.ContentStream = content;

			using (HttpClientResponse response = this.Dispatch(lRequest))
				return response.ContentString;
		}
Esempio n. 8
0
		public HttpClientResponse GetResponse(String url)
		{
			HttpClientRequest lRequest = new HttpClientRequest();
			lRequest.Url.Parse(url);
			lRequest.Header.RequestType = "GET";
			lRequest.Header.SetHeaderValue("Accept", Accept);
			lRequest.Header.SetHeaderValue("User-Agent", UserAgent);
			lRequest.KeepAlive = KeepAlive;

			return this.Dispatch(lRequest);
		}
Esempio n. 9
0
		private void btnSubmit_Click(object sender, EventArgs e)
		{
			HttpClientRequest request = new HttpClientRequest();
			if (rbPost.Checked) // don't set .Get, it's the default
			{
				request.RequestType = RequestType.Post;

				string contentString = "";
				for (int i = 0; i < tblParams.Rows.Count; i++)
				{
					DataRow currentRow = tblParams.Rows[i];

					contentString += string.Format("{0}={1}\r\n", currentRow["Name"], currentRow["Value"]);
				}

				request.ContentString = contentString;
			}

			request.Url.Parse(edUrl.Text);

			// set headers
			for (int i = 0; i < tblHeaders.Rows.Count; i++)
			{
				DataRow aRow = tblHeaders.Rows[i];
				request.Header.SetHeaderValue(aRow["Name"].ToString(), aRow["Value"].ToString());
			}

			httpClient1.KeepAlive = cbKeepAlive.Checked;
			request.KeepAlive = httpClient1.KeepAlive;

			tblResponseHeaders.Clear();
			edResult.Text = "";

			tabControl1.SelectedIndex = 1;
			Application.DoEvents();

			try
			{
				HttpClientResponse lResponse = httpClient1.Dispatch(request);
				ShowResponse(lResponse);
			}
			catch (HttpException ex)
			{
				ShowResponse(ex.Response);
			}
			catch (Exception ex)
			{
				_LastResultString = "Error retrieving response: " + ex.Message;
				_LastResultBytes = new UnicodeEncoding().GetBytes(_LastResultString);
				_LastLength = _LastResultBytes.Length;
				SetResultText();
			}
		}
Esempio n. 10
0
        public HttpClientResponse TryDispatch(HttpClientRequest request)
        {
            HttpClient.SetAuthorizationHeader(request.Header, "Authorization", this.UserName, this.Password);

            String  lHostname      = request.URL.Host;
            Int32   lPort          = request.URL.Port;
            Boolean lSslConnection = request.URL.Scheme?.ToLower() == "https";

            // Settings for connection thru Http Proxy
            // Note that Request should think that it uses direct connection when SSL is enabled because
            // proxy server tunnels SSL data AS IS, without adjusting its HTTP headers
            request.UseProxy = this.ProxySettings.UseProxy && !lSslConnection;
            if (this.ProxySettings.UseProxy)
            {
                lHostname = this.ProxySettings.ProxyHost;
                lPort     = this.ProxySettings.ProxyPort;

                HttpClient.SetAuthorizationHeader(request.Header, "Proxy-Authorization", this.ProxySettings.UserName, this.ProxySettings.Password);
            }

            Connection lConnection = this.GetHttpConnection(lSslConnection, request.URL.Host, request.URL.Port, lHostname, lPort);

            try
            {
                request.WriteHeaderToConnection(lConnection);
            }
            catch (ObjectDisposedException)
            {
                lConnection = this.GetNewHttpConnection(lHostname, lPort);
                request.WriteHeaderToConnection(lConnection);
            }
            catch (ConnectionClosedException)
            {
                lConnection = this.GetNewHttpConnection(lHostname, lPort);
                request.WriteHeaderToConnection(lConnection);
            }
            catch (SocketException)
            {
                lConnection = this.GetNewHttpConnection(lHostname, lPort);
                request.WriteHeaderToConnection(lConnection);
            }

            request.WriteBodyToConnection(lConnection);

            HttpClientResponse lResponse;

            do
            {
                HttpHeaders lHeaders = HttpHeaders.Create(lConnection);
                if (lHeaders == null)
                {
                    throw new ConnectionClosedException();
                }
                lResponse = new HttpClientResponse(lConnection, lHeaders);
            }while (lResponse.Header.HttpCode == HttpStatusCode.Continue);            // 100 CONTINUE means useless response.

            if (!lResponse.KeepAlive)
            {
                this.fConnectionUrl = null;
                this.fConnection    = null;
            }

            if (lResponse.HttpCode == HttpStatusCode.ProxyAuthenticationRequired)
            {
                throw new HttpException("Proxy authorization failed", lResponse);
            }

            return(lResponse);
        }