How do I see the raw HTTP request that the HttpWebRequest class sends?
You can use System.Net tracing mechanism to see the raw HTTP requests sent on the wire. You can also add your own tracelistener to the process.
Logging raw HTTP request/response in ASP.NET MVC & IIS7
OK, so it looks like the answer is "no you can't get the raw data, you have to reconstruct the request/response from the properties of the parsed objects". Oh well, I've done the reconstruction thing.
View contents of HttpWebRequest without tools(C#)
Try using an HTTP debugging tool like Fiddler. It acts like a proxy and can let you view the request and response of HTTP requests your program sends.
Here's how to use it with HttpWebRequest: Get HTTP requests and responses made using HttpWebRequest/HttpWebResponse to show in Fiddler
If you aren't allowed to install Fiddler you could think about making your own version of Fiddler in c# to capture traffic How to create a simple proxy in C#?
How do I get the raw request body from the Request.Content object using .net 4 api endpoint
You can get the raw data by calling ReadAsStringAsAsync
on the Request.Content
property.
string result = await Request.Content.ReadAsStringAsync();
There are various overloads if you want it in a byte or in a stream. Since these are async-methods you need to make sure your controller is async:
public async Task<IHttpActionResult> GetSomething()
{
var rawMessage = await Request.Content.ReadAsStringAsync();
// ...
return Ok();
}
EDIT: if you're receiving an empty string from this method, it means something else has already read it. When it does that, it leaves the pointer at the end. An alternative method of doing this is as follows:
public IHttpActionResult GetSomething()
{
var reader = new StreamReader(Request.Body);
reader.BaseStream.Seek(0, SeekOrigin.Begin);
var rawMessage = reader.ReadToEnd();
return Ok();
}
In this case, your endpoint doesn't need to be async (unless you have other async-methods)
send and receive data through raw body of a WebRequest using wcf restful service?
The problem is that with POSTMAN you send:
{"data":{"IMEI":"352423061590616"}}
but when you inspect the serialized data from
JavaScriptSerializer ser = new JavaScriptSerializer();
DeviceData data = new DeviceData() { IMEI = "352423061590616" };
string str = ser.Serialize(data);
You'll see that the wireformat is:
{"IMEI":"352423061590616"}
And that doesn't map well on your data
argument.
To resolve this add an extra class for the purpose of Root container. It has a data property from the type DeviceData
to hold your actual payload. The code to serialize would look like this:
// container with a data property
// this one is being serialized
public class Root
{
public DeviceData data { get; set; }
}
public class DeviceData
{
public string IMEI { get; set; }
}
protected void CallIMEIVerfication()
{
JavaScriptSerializer ser = new JavaScriptSerializer();
DeviceData data = new DeviceData() { IMEI = "352423061590616" };
var root = new Root { data=data };
string str = ser.Serialize(root);
// rest of your code
}
Detailed timing info from HttpWebRequest
For future reference, I'm answering my own question. While the proposed answer with the logger might work, I've found a better (IMO) and much simpler approach:
var waiting = new Stopwatch();
var contentDownload = new Stopwatch();
waiting.Start();
using (var webResponse = (HttpWebResponse)webRequest.GetResponse())
{
waiting.Stop();
contentDownload.Start();
using (var reader = new StreamReader(webResponse.GetResponseStream()))
{
var body = reader.ReadToEnd();
contentDownload.Stop();
}
}
It's as simple as that really. Calling GetResponse
corresponds Chrome's Waiting and GetResponseStream
+ReadToEnd
corresponds Content Download.
In this example I'm making a GET (body-less) request. Being able to time Request Sent would make sense, but not really achievable using this approach.
How to get the raw content of a response in requests with Python?
If you are using a requests.get
call to obtain your HTTP response, you can use the raw
attribute of the response. Here is the code from the requests
docs. The stream=True
parameter in the requests.get
call is required for this to work.
>>> r = requests.get('https://github.com/timeline.json', stream=True)
>>> r.raw
<requests.packages.urllib3.response.HTTPResponse object at 0x101194810>
>>> r.raw.read(10)
'\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\x03'
Send HTTP POST request in .NET
There are several ways to perform HTTP GET and POST requests:
Method A: HttpClient (Preferred)
Available in: .NET Framework 4.5+, .NET Standard 1.1+, and .NET Core 1.0+.
It is currently the preferred approach, and is asynchronous and high performance. Use the built-in version in most cases, but for very old platforms there is a NuGet package.
using System.Net.Http;
Setup
It is recommended to instantiate one HttpClient
for your application's lifetime and share it unless you have a specific reason not to.
private static readonly HttpClient client = new HttpClient();
See HttpClientFactory
for a dependency injection solution.
POST
var values = new Dictionary<string, string>
{
{ "thing1", "hello" },
{ "thing2", "world" }
};
var content = new FormUrlEncodedContent(values);
var response = await client.PostAsync("http://www.example.com/recepticle.aspx", content);
var responseString = await response.Content.ReadAsStringAsync();GET
var responseString = await client.GetStringAsync("http://www.example.com/recepticle.aspx");
Method B: Third-Party Libraries
RestSharp
POST
var client = new RestClient("http://example.com");
// client.Authenticator = new HttpBasicAuthenticator(username, password);
var request = new RestRequest("resource/{id}");
request.AddParameter("thing1", "Hello");
request.AddParameter("thing2", "world");
request.AddHeader("header", "value");
request.AddFile("file", path);
var response = client.Post(request);
var content = response.Content; // Raw content as string
var response2 = client.Post<Person>(request);
var name = response2.Data.Name;
Flurl.Http
It is a newer library sporting a fluent API, testing helpers, uses HttpClient under the hood, and is portable. It is available via NuGet.
using Flurl.Http;
POST
var responseString = await "http://www.example.com/recepticle.aspx"
.PostUrlEncodedAsync(new { thing1 = "hello", thing2 = "world" })
.ReceiveString();GET
var responseString = await "http://www.example.com/recepticle.aspx"
.GetStringAsync();
Method C: HttpWebRequest (not recommended for new work)
Available in: .NET Framework 1.1+, .NET Standard 2.0+, .NET Core 1.0+. In .NET Core, it is mostly for compatibility -- it wraps HttpClient
, is less performant, and won't get new features.
using System.Net;
using System.Text; // For class Encoding
using System.IO; // For StreamReader
POST
var request = (HttpWebRequest)WebRequest.Create("http://www.example.com/recepticle.aspx");
var postData = "thing1=" + Uri.EscapeDataString("hello");
postData += "&thing2=" + Uri.EscapeDataString("world");
var data = Encoding.ASCII.GetBytes(postData);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = data.Length;
using (var stream = request.GetRequestStream())
{
stream.Write(data, 0, data.Length);
}
var response = (HttpWebResponse)request.GetResponse();
var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();GET
var request = (HttpWebRequest)WebRequest.Create("http://www.example.com/recepticle.aspx");
var response = (HttpWebResponse)request.GetResponse();
var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
Method D: WebClient (Not recommended for new work)
This is a wrapper around HttpWebRequest
. Compare with HttpClient
.
Available in: .NET Framework 1.1+, NET Standard 2.0+, and .NET Core 2.0+.
In some circumstances (.NET Framework 4.5-4.8), if you need to do a HTTP request synchronously, WebClient
can still be used.
using System.Net;
using System.Collections.Specialized;
POST
using (var client = new WebClient())
{
var values = new NameValueCollection();
values["thing1"] = "hello";
values["thing2"] = "world";
var response = client.UploadValues("http://www.example.com/recepticle.aspx", values);
var responseString = Encoding.Default.GetString(response);
}GET
using (var client = new WebClient())
{
var responseString = client.DownloadString("http://www.example.com/recepticle.aspx");
}
Related Topics
Float/Double Precision in Debug/Release Modes
How to Hide an Inherited Property in a Class Without Modifying the Inherited Class (Base Class)
Use Dependency Injection in Static Class
C# Winforms: How to Set Main Function Stathreadattribute
Get the Id of Inserted Row Using C#
Generic C# Code and the Plus Operator
Using Tfs API, How to Find the Comments Which Were Made on a Code Review
Interesting "Params of Ref" Feature, Any Workarounds
ASP.NET How to Stream File to User
Rendering Partial Views Using Ajax
Stream.Seek(0, Seekorigin.Begin) or Position = 0
Will the Base Class Constructor Be Automatically Called