2012-04-24 43 views
0

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

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

我遇到的问题是,当我创造它不是在订购网站收到的context.Response一个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()); 
} 

回答

1

尝试加入这一行:

webRequest.CookieContainer = new CookieContainer(); 

之后

webRequest.ContentLength = bytes.Length;