0

我有一个订购网站,需要在供应商网站上提出设置请求。为此,我正在使用 WebHanlder(ashx 文件)来使用工作正常的 HttpContext 对象读取 cXML 中的设置请求。

其中一项要求是我们发送回一个名为“Buyer Cookie”的 Cookie 以及 cXML 200 OK 响应。

我遇到的问题是当我在上下文中创建一个 cookie 时。响应它在我执行时没有在订购站点中收到response.Output.Write().

response.Flush()在写入后尝试使用,但仍然无法正常工作

如何将 cookie 发送回调用站点?

这是我的代码:

订购网站

Stream stream = null;
byte[] bytes = Encoding.ASCII.GetBytes(File.ReadAllText(@"D:\Prototypes\HTTPPost\cXMLFiles\PunchOutSetupRequest.xml"));
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create("http://localhost:45454/PunchOutRequest.ashx");

webRequest.ContentType = "application/x-www-form-urlencoded";
webRequest.Method = "POST";
webRequest.ContentLength = bytes.Length;

try
{
    stream = webRequest.GetRequestStream();
    stream.Write(bytes, 0, bytes.Length);
}
catch (Exception)
{
    throw;
}
finally
{
    if (stream != null)
        stream.Close();
}

HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse();
Stream responseStream = response.GetResponseStream();
StreamReader responseReader = new StreamReader(responseStream);

string r = responseReader.ReadToEnd();
var buy = webRequest.CookieContainer;
var buyer = response.Cookies["BuyerCookie"]; // This is always null

供应商网站

var request = context.Request;
StreamReader reader = new StreamReader(request.InputStream);

string text = reader.ReadToEnd();

POSetup setup = new POSetup();

if (setup.IsSetupRequestValid(text))
{
    HttpCookie cookie = new HttpCookie("BuyerCookie", "100");

    context.Response.Cookies.Add(cookie);
    context.Response.Output.Write(setup.GetOKResponse());
}
4

1 回答 1

1

尝试添加这一行:

webRequest.CookieContainer = new CookieContainer();

紧接着

webRequest.ContentLength = bytes.Length;
于 2012-04-24T17:55:03.503 回答