Click here to Skip to main content
15,942,934 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Hello All,

I am creating a ASP.NET Web project, where a third party will hit one of my page (using HTML Form Post) and my page will return some details back to the third party using the HttpWebRequest POST method, the third party will resend all the data received with a key word, so my site can confirm the valid hit and return a confirmation to the third party about the POST data received.

This flow is something like used by Payment Gateways. My problem is, what code should be there on my page so the flow returns back to the third party web page, when they are using the following code to get a response from my site.

C#
byte[] post_buffer = Encoding.UTF8.GetBytes(<DataToPost>);

//Create and initialize the request
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = post_buffer.Length;
Stream request_stream = request.GetRequestStream();
request_stream.Write(postBuffer, 0, postBuffer.Length);
request_stream.Close();
postBuffer = null;

//send the request, read the response
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream response_stream = response.GetResponseStream();
Encoding encode = System.Text.Encoding.GetEncoding("utf-8");
StreamReader read_stream = new StreamReader(response_stream, encode);


EdMan196 Edit : Added pre block, unchecked "Treat my content as plain text, not as HTML"
Posted
Updated 20-Feb-12 2:37am
v2
Comments
Ed Nutting 20-Feb-12 8:34am    
Just create a normal aspx page for them to request to and then process as usual - you don't need to do anything special. If you want to return XML data (or another format) simply create an aspx page then in the load event do Response.Clear(); Response.ContentType = "text/xml"; and after using Response.Write to write all your output (still in the load event) put a Response.End() to prevent any of the actual ASPX page from being processed/outputted.

Edit: To get the information they sent to you, use Request.Form["TheIDOfTheDataYouWantedToGet"]

Hope this helps,
Ed
Jaydeep Chatterjee 20-Feb-12 8:44am    
It worked :-)
Thanks a lot Edward.
Ed Nutting 20-Feb-12 9:05am    
always happy to help :)

1 solution

For any who may come looking, see my comments on the original question for the solution.

To OP: Please mark this as the accepted solution so that others know it is solved.

Thanks very much,
Ed
 
Share this answer
 
Comments
Jaydeep Chatterjee 1-Mar-12 8:39am    
Hello Ed,

Need you help again, this is continuation of the above Q. This is the problem in my site when sending a HTTPWebRequest. I need to redirect to a page just after sending the request.

HttpWebRequest _WebRequest = (HttpWebRequest)WebRequest.Create(_NotifyURL);
_WebRequest.AllowAutoRedirect = true;
_WebRequest.Method ="POST";
_WebRequest.ContentType ="application/x-www-form-urlencoded";
_WebRequest.ContentLength = _Buffer.Length;
Stream _WebRequestStream = _WebRequest.GetRequestStream();
_WebRequestStream.Write(_Buffer, 0, _Buffer.Length);
_Buffer = null;
_WebRequestStream.Close();

Response.Redirect(_ReturnURL, true);

The above line throws an exception saying "{System.Exception} = {Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack.}"

Can you please help.
Ed Nutting 1-Mar-12 11:26am    
Hi there, the exception you have detailed isn't the real exception. the real exception is a thread abort exception. response.redirect throws a thread abort exception whenever it is called to stop processing of the current request. what you need is a the following around your redirect line:


try
{
Response.Redirect(_ReturnURL, true);
}
catch (ThreadAbortException)
{
}


What this will do is only catch the thread abort exception meaning your code will work properly unless a different error occurs.

Hope this solves your issue, if not put an extra catch block to catch the different exception and post what it is.
Ed

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900