简体   繁体   中英

HTTP Post from ASP.NET to jsp

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM