How to handle an HTTP post

Russ Suter
Russ Suter used Ask the Experts™
on
I feel so sheepish. I've worked with ASP.NET for years but never used this before. I have code that submits an HTTP post just fine to an external service. What I need to do is insert my own HTTP post handling in between to perform some additional checking and logging. I can send the request just fine using the following code:
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://externalservice.com/Test/version3.2");

            string postData = "parameter_1=foo&parameter_2=bar";
            byte[] data = Encoding.UTF8.GetBytes(postData);

            request.Method = "POST";
            request.ContentType = "application/x-www-form-urlencoded";
            request.ContentLength = data.Length;
            request.UserAgent = "TestAgent/Version:2015.Sep.22";

            using (Stream stream = request.GetRequestStream())
            {
                stream.Write(data, 0, data.Length);
            }

            HttpWebResponse response = (HttpWebResponse)request.GetResponse();

            string responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();

Open in new window

I need to replace "http://externalservice.com/Test/version3.2" with "http://localhost/Test/version3.2" so I can inspect the request, log it, and forward it to the original URL but I'm running into 404 and 405 errors. I had assumed I could just create a page called version3.2.aspx in the ~/Test folder but that's not working. What am I doing wrong? Better question, how do I do it right?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
ste5anSenior Developer

Commented:
What are you trying here? To invoke a web service? If so, what kind of web service (REST or SOAP)?
Hi  

As it seems you only need to inspect data, and not add aby logic, then creating a web service yourself just for that purpose seems to be too much. Not to mention that you would fill your project with garbage.

I would suggest using a tool like Fiddler, which can inspect requests leaving your pc. That would give you all information you would need.

If by any chance, you would still need to create your own service, i would suggest to create a new web api project, add an ApiController named TestController and add a method named Version32 with an attribute [HttpPost] above the method declaration.

I would strongly recomend the first option though.

Giannis
Commented:
If you just want to check the post, Fiddler (as Gianni's said) is clearly the best option as it is non-intrusive.
if you want to have the option to alter the post too, then creating a local web service is the way to go. Using the original wsdl, you can create a stub that can consume your call and forward it.

Bear in mind that the more advanced applications will not allow a middle tier like this to avoid tampering with the data.

Dennis

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial