Reputation: 3523
I am trying to do a HTTP POST from ASP.NET and JSP and it is not working.
I have been reading articles on how to do a HTTP POST in C# and have come accross code snippets like the example I have written below using HttpWebRequest:
Stream stream = null;
byte[] bytes = Encoding.ASCII.GetBytes(RendercXMLForPosting(cXMLContent));
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(ConfigurationManager.AppSettings["Address"]);
webRequest.ContentType = "application/x-www-form-urlencoded";
webRequest.Method = "POST";
webRequest.ContentLength = bytes.Length;
webRequest.CookieContainer = new CookieContainer();
webRequest.CookieContainer.Add(new Cookie("BuyerCoookie", punchOutSession.BuyerCookieID, "/", ConfigurationManager.AppSettings["Domain"]));
try
{
stream = webRequest.GetRequestStream();
stream.Write(bytes, 0, bytes.Length);
}
catch (Exception)
{
throw;
}
finally
{
if (stream != null)
stream.Close();
}
When I try this no error is thrown but The 3rd party site is not recognising the POST, the 3rd Party site is a JSP site.
Is this the wrong way to post to a JSP site from ASP.NET? Is there something I am missing? Thanks in advance
EDIT!!! I need to redirectthe user to the POSTing page after the post is complete, any help on that?
Upvotes: 0
Views: 1608
Reputation: 5679
Try flushing the request stream, and getting the response from the request, ie:
stream = webRequest.GetRequestStream();
stream.Write(bytes, 0, bytes.Length);
stream.Flush
var rsp = webRequest.GetResponse();
using(var sr = new StreamReader(rsp.GetResponseStream())
var result = sr.ReadToEnd(); // you might want to see what this is to debug
Upvotes: 2