2015-05-07 80 views
0

我想發送一個從Google Chromes網絡F12選項卡中提取的cookie,以預先在HTTP請求中輸入用戶界面。但是,我需要發送與請求一起發送的cookie。我似乎無法將cookie字符串附加到請求中。從谷歌瀏覽器(F12) http://imgur.com/8wEaSSC.pngC#HttpRequest附加Cookie請求

捕捉我需要該cookie連接請求時,我送出來。

 string cookie = "JSESSIONID_loginLite_remote=LJyKVLHGLZrpTq3npBWh8CJyqTND32HpscL48ndnSp0hj2g6yhcK!....."; 
     string info = "RemoteObjectTimestamp=1430842283795&RemoteObjectRequest=%7B%22destination%22%..."; 

     char[] arr  = info.ToCharArray(); 
     byte[] data  = System.Text.Encoding.ASCII.GetBytes(arr); 

     HttpWebRequest myHttpWebRequest = (HttpWebRequest)HttpWebRequest.Create(URL); 
     myHttpWebRequest.Method   = "POST"; 
     myHttpWebRequest.ContentType = "application/x-www-form-urlencoded;charset=UTF-8"; 
     myHttpWebRequest.ContentLength = data.Length; 

     //Data to write is not empty 
     if (data.Length > 0) 
     { 
      Stream requestStream = myHttpWebRequest.GetRequestStream(); 
      requestStream.Write(data, 0, data.Length); 
      requestStream.Close(); 
     } 

     HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse(); 

     Stream responseStream = myHttpWebResponse.GetResponseStream(); 
     StreamReader myStreamReader = new StreamReader(responseStream, System.Text.Encoding.Default); 
     string pageContent = myStreamReader.ReadToEnd(); 

     myStreamReader.Close(); 
     responseStream.Close(); 
     myHttpWebResponse.Close(); 

     Console.WriteLine(pageContent); 

回答

0

添加行:

myHttpWebRequest.CookieContainer = new CookieContainer(); 
myHttpWebRequest.CookieContainer.SetCookies(new Uri("your domain here"), cookie); 

唐`忘記更換 「這裏域」,以目標請求URI

+0

我沒有一個cookie的域。我有一個包含多個部分的巨型字符串。所以我需要將巨型字符串轉換爲一個cookie,然後我可以將它添加到請求中。 – Shane

+0

你有沒有試圖把你的巨大的字符串放在上面的方法中的「cookie」變量中?這是個問題嗎? –