We help IT Professionals succeed at work.
Get Started

How to handle an HTTP post

Russ Suter
Russ Suter asked
Last Modified: 2015-09-27
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?
Watch Question
CEO @ Arkro IT
This problem has been solved!
Unlock 1 Answer and 3 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE