Avatar of Russ Suter
Russ Suter
 asked on

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?
.NET ProgrammingASP.NETC#

Avatar of undefined
Last Comment
Dennis Aries

8/22/2022 - Mon
ste5an

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

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
ASKER CERTIFIED SOLUTION
Dennis Aries

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck