C# IHttpHandler将cookie发送回呼叫网站
我有一个订购网站,需要在供应商网站上提出设置请求。 为此,我使用一个WebHanlder(ashx文件)来读取cXML中的安装请求,使用HttpContext对象,该对象工作正常 其中一个要求是,我们返回一个名为“买方Cookie”的Cookie以及一个CXML200OK响应 我遇到的问题是,当我在context.Response中创建cookie时,当我执行C# IHttpHandler将cookie发送回呼叫网站,c#,asp.net,cookies,httphandler,C#,Asp.net,Cookies,Httphandler,我有一个订购网站,需要在供应商网站上提出设置请求。 为此,我使用一个WebHanlder(ashx文件)来读取cXML中的安装请求,使用HttpContext对象,该对象工作正常 其中一个要求是,我们返回一个名为“买方Cookie”的Cookie以及一个CXML200OK响应 我遇到的问题是,当我在context.Response中创建cookie时,当我执行Response.Output.Write()。 我尝试在写入后使用response.Flush(),但仍然不起作用 如何将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());
}
供应商站点
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;