0

ASP.NETおよびJSPからHTTPPOSTを実行しようとしていますが、機能しません。

私は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();
}

これを試してみると、エラーはスローされませんが、サードパーティのサイトがPOSTを認識していないため、サードパーティのサイトはJSPサイトです。

これは、ASP.NETからJSPサイトに投稿する間違った方法ですか?足りないものはありますか?前もって感謝します

編集!!! 投稿が完了した後、ユーザーをPOSTページにリダイレクトする必要があります。それに関するヘルプはありますか?

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 に答える