How to handle an HTTP post

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?
LVL 22
Russ SuterSenior Software DeveloperAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ste5anSenior DeveloperCommented:
What are you trying here? To invoke a web service? If so, what kind of web service (REST or SOAP)?
Ioannis ParaskevopoulosCommented:
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
Dennis AriesCEO @ Arkro ITCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.