Наследование: HttpOutgoingRequestResponse
Пример #1
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;
		}
Пример #2
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;
		}
Пример #3
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);
		}
Пример #4
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;
		}
Пример #5
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();
			}
		}