0

I have an ordering website that needs to make a set up request on a supplier site. For this i am using a WebHanlder (ashx file) to read the setup request in cXML using the HttpContext object which is working fine.

One of the requirements is that we send back a Cookie called "Buyer Cookie" along with a cXML 200 OK response.

The problem I am having is when I create a cookie in the context.Response it is not recieved in the ordering site when I do the response.Output.Write().

I have tried using response.Flush() after the write and this is still not working

How can I send a cookie back to the calling site?

Here is my code:

Ordering site

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

Supplier Site

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());
}
huMpty duMpty
  • 14,346
  • 14
  • 60
  • 99
Andy Clark
  • 3,363
  • 7
  • 27
  • 42

1 Answers1

1

Try adding this line:

webRequest.CookieContainer = new CookieContainer();

right after

webRequest.ContentLength = bytes.Length;
aquinas
  • 23,318
  • 5
  • 58
  • 81