Link to home
Start Free TrialLog in
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?
Avatar of ste5an
ste5an
Flag of Germany image

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
ASKER CERTIFIED SOLUTION
Avatar of Dennis Aries
Dennis Aries
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial