Send POST request to asp.net mvc action via Fiddle

2019-03-10 19:55发布

问题:

I have an ASP.NET MVC web site. One of my routes is a URL that takes 5 parameters. For the sake of illustration, these parameters are named parameter1, parameter2, parameter3, parameter4, and parameter5. Currently, I'm constructing a URL in some C# code that will POST to the mvc action via a WebClient. that code looks like this:

WebClient myWebClient = new WebClient();
myWebClient.UploadStringCompleted += myWebClient_UploadStringCompleted;

string url = "http://www.example.com/customer/" + parameter1 + "/orders/" + parameter2 + "/" + parameter3 + "/" + parameter4 + "/" + parameter5;
myWebClient.UploadStringAsync(new Uri(url, UriKind.Absolute));

I'm confident that the UploadString method does a POST. I need to do a POST, because my parameter values can be very long. In fact, I estimate that occasionally, the total url length may be 20000 characters long. Regardless, I get a 400 error when I attempt to post my data. In an effort to debug this, I'm trying to figure out how to simulate a POST in Fiddler.

Assuming that I am passing values via a query string as shown above, what values do I enter into Fiddler? From the Composer tab, I'm not sure what to enter for the Request Headers area. I'm also not entirely sure what to enter for the url. I'm not sure if I put the entire URL in there, including the parameter values, or if those belong in the Request Headers.

What I need to enter into Fiddler, so that I can debug my issue?

回答1:

Basically all your parameters are a part of the URL, and this is the root of your problem. Here is what is going on: you are hitting the URL length limitation, and receiving a "400 Bad request" error. In real world most web browsers do not work with URLs more than 2000 characters long.

To resolve this problem I would suggest doing a bit of refactoring, so that request is posted to the URL http://www.example.com/customer/parameter1/orders or even http://www.example.com/customer/orders with parameters send in request body. Here is how test such request in Fiddler:

  1. On Composer tab choose POST request verb
  2. Specify the URL as

    http://www.example.com/customer/parameter1/orders

    or

    http://www.example.com/customer/orders

  3. In Request Headers section you can set content type header like

    Content-Type: application/x-www-form-urlencoded
    

    or any other header you might require. Or just leave it blank which will work in your case.

  4. Finally in Request Body field list your parameters in query string form

    parameter1name=parameter1value&parameter2name=parameter2value
    

In this new case here is how you can send such a request using WebClient:

WebClient myWebClient = new WebClient();
myWebClient.UploadStringCompleted += myWebClient_UploadStringCompleted;

string url = "http://www.example.com/customer/orders";
string data = "parameter1name=parameter1value&parameter2name=parameter2value";

myWebClient.UploadStringAsync(new Uri(url, UriKind.Absolute), data);


回答2:

I simply mimic the exact request that was sent.

This is how I do it:

  1. Open Fiddler
  2. Go to the page that I want to re-issue the command i.e. repeat the bug step but watch for the request in the list
  3. Select it from the list and right-click, go to replay > reissue and edit
  4. This build a replicated request but hits a break point before it is sent (You will see the red bar on the right)
  5. Above this you can edit the values that were sent by double-clicking on any of them in Headers, QueryString etc
  6. Then hit Run to Complete