Here is the code I use to catch the Request and Response headers:
* This is taken from a Windows Forms application, therefore: txtReceived and txtSent are simple multi-line text fields in the form of WindowsForms. and the cursor is the shape cursor. *
private HttpClient PrepareHttpClient()
{
var client = new HttpClient();
client.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue("application/xml"));
return client;
}
private string SendGetRequest(string url)
{
this.Cursor = Cursors.WaitCursor;
var client = PrepareHttpClient();
txtSent.Text = url;
var taskReult = client.GetAsync(new Uri(url));
HttpResponseMessage httpResponse = taskReult.Result;
Stream st = httpResponse.Content.ReadAsStreamAsync().Result;
StreamReader reader = new StreamReader(st);
string content = reader.ReadToEnd();
this.Cursor = Cursors.Default;
txtReceived.Text = FormatResponse(httpResponse, content);
if (httpResponse.StatusCode == HttpStatusCode.OK)
{
return content;
}
throw new ApplicationException(content);
}
private string SendPostRequest<T>(string url, T e)
{
this.Cursor = Cursors.WaitCursor;
HttpClient client = new HttpClient();
MediaTypeFormatter jsonFormatter = new JsonMediaTypeFormatter();
HttpContent content = new ObjectContent<T>(e, jsonFormatter);
Stream st = content.ReadAsStreamAsync().Result;
StreamReader reader = new StreamReader(st);
string s = reader.ReadToEnd();
var taskResult = client.PostAsync(url, content);
HttpResponseMessage httpResponse = taskResult.Result;
this.Cursor = Cursors.Default;
txtSent.Text = FormatRequest(httpResponse.RequestMessage, s);
st = httpResponse.Content.ReadAsStreamAsync().Result;
reader = new StreamReader(st);
string responseContent = reader.ReadToEnd();
txtReceived.Text = FormatResponse(httpResponse, responseContent);
if (httpResponse.StatusCode == HttpStatusCode.Created)
{
return responseContent;
}
throw new ApplicationException(responseContent);
}
private string SendPutRequest<T>(string url, T e)
{
this.Cursor = Cursors.WaitCursor;
HttpClient client = new HttpClient();
MediaTypeFormatter jsonFormatter = new JsonMediaTypeFormatter();
HttpContent content = new ObjectContent<T>(e, jsonFormatter);
Stream st = content.ReadAsStreamAsync().Result;
StreamReader reader = new StreamReader(st);
string s = reader.ReadToEnd();
var taskResult = client.PutAsync(url, content);
HttpResponseMessage httpResponse = taskResult.Result;
txtSent.Text = FormatRequest(httpResponse.RequestMessage, s);
st = httpResponse.Content.ReadAsStreamAsync().Result;
reader = new StreamReader(st);
string responseContent = reader.ReadToEnd();
this.Cursor = Cursors.Default;
txtReceived.Text = FormatResponse(httpResponse, responseContent);
if (httpResponse.StatusCode == HttpStatusCode.OK)
{
return responseContent;
}
throw new ApplicationException(responseContent);
}
private string FormatRequest(HttpRequestMessage request, string content)
{
return
string.Format("{0} {1} HTTP/{2}\r\n{3}\r\n{4}",
request.Method,
request.RequestUri,
request.Version,
request.Headers,
content);
}
private string FormatResponse(HttpResponseMessage result, string content)
{
return
string.Format("HTTP/{0} {1} {2}\r\n{3}\r\n{4}",
result.Version,
(int)result.StatusCode,
result.ReasonPhrase,
result.Headers,
content);
}
Drror source
share