0

我正在尝试从 ASP.NET 和 JSP 执行 HTTP POST,但它不工作。

我一直在阅读有关如何在 C# 中执行 HTTP POST 的文章,并且遇到了代码片段,例如我在下面使用 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();
}

当我尝试这个时,没有抛出错误,但第 3 方站点无法识别 POST,第 3 方站点是 JSP 站点。

这是从 ASP.NET 发布到 JSP 站点的错误方式吗?有什么我想念的吗?提前致谢

编辑!!! 帖子完成后,我需要将用户重定向到 POSTing 页面,有什么帮助吗?

4

1 回答 1

2

尝试刷新请求流,并从请求中获取响应,即:

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
于 2012-05-23T17:41:12.583 回答