IHttpHandler 将 cookie 发送回调用网站
本文关键字:回调 调用 网站 cookie IHttpHandler | 更新日期: 2023-09-27 18:32:10
>我有一个订购网站,需要在供应商网站上提出设置请求。为此,我正在使用WebHanlder(ashx文件)使用HttpContext对象读取cXML中的安装请求,该对象工作正常。
其中一个要求是我们发回一个名为"BuyerCookie"的 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());
}
尝试添加以下行:
webRequest.CookieContainer = new CookieContainer();
紧接着
webRequest.ContentLength = bytes.Length;