62,041
社区成员
发帖
与我相关
我的任务
分享
/// <summary>
/// 获取购物车信息
/// </summary>
/// <returns></returns>
public static string GetShoppingInfo() {
HttpCookie hc = null;
hc = HttpContext.Current.Request.Cookies[CookieName];
if (hc == null) //如果Cookie存在 则直接新建Cookie 保存商品id和数量;
{
return null;
}
else {
return hc.Value;
}
}
HttpWebRequest myRequest = (HttpWebRequest)HttpWebRequest.Create(requestURL);
myRequest.Method = "post";
//开始提取站点cookie的信息
String[] arr1 = Request.Cookies.AllKeys;
StringBuilder cookieStr = new StringBuilder();
for (int i = 0; i < arr1.Length; i++)
{
HttpCookie hc = Request.Cookies[arr1[i]];
if ((hc.Name != null) && (hc.Value != null))
{
cookieStr.Append(hc.Name + ",");
}
}
if (cookieStr.Length > 1)
{
cookieStr.Remove(cookieStr.Length - 1, 1);
}
Uri uri = new Uri(ConfigurationHelper.GetAppSettings("Uri_Value"));
//CookieContainer使用之前必须new一个,否则是NULL的
myRequest.CookieContainer = new CookieContainer();
//在SetCookie方法中,传入的Cookie是站点cookie的key集合,通过逗号分隔
myRequest.CookieContainer.SetCookies(uri, cookieStr.ToString());
myRequest.Timeout = 300000;
HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();