2011-09-05 49 views
0

好了,所以我有一個功能,我寫道,應該允許我使用cookie數據發佈。問題在於我在亞馬遜登錄頁面上進行了測試,並且一直在回覆說我需要啓用Cookie。下面的代碼C#後數據的Cookie不工作

public string DoPost(String url, PostData data, CookieContainer cookies) 
{ 
    HttpWebRequest objWebRequest = (HttpWebRequest)WebRequest.Create(url); 
    objWebRequest.CookieContainer = cookies; 
    objWebRequest.AllowAutoRedirect = true; 
    objWebRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)"; 
    if(data != null) 
    { 
     String post = data.ToString(); 
     objWebRequest.Method = "POST"; 
     objWebRequest.ContentLength = post.Length; 
     objWebRequest.ContentType = "application/x-www-form-urlencoded"; 
     // Post to the login form. 
     using(StreamWriter swRequestWriter = new StreamWriter(objWebRequest.GetRequestStream())) 
     { 
      swRequestWriter.Write(post); 
     } 
    } 

    // Get the response. 
    HttpWebResponse objWebResponse = 
     (HttpWebResponse)objWebRequest.GetResponse(); 

    // Read the response 
    using(StreamReader srResponseReader = new StreamReader(objWebResponse.GetResponseStream())) 
    { 
     string strResponseData = srResponseReader.ReadToEnd(); 
     return strResponseData; 
    } 
} 

我這樣稱呼它

String action = "https://www.amazon.com/gp/flex/sign-in/select.html"; 
String s = DoPost(action, null, Cookies); 

餅乾在我班上建築工創建這樣

CookieContainer Cookies; 

public Constructz0r() 
{ 
    Cookies = new CookieContainer(); 
} 

的事情是,我還沒有張貼任何發佈數據,我只是去頁面,它說我的cookies沒有啓用,雖然我覺得我已經做了它在DoPost寫。

我已經使用這個實現的WebClient

public class CookieWebClient : WebClient 
    { 
     private CookieContainer _cookieContainer = new CookieContainer(); 

     protected override WebRequest GetWebRequest(Uri address) 
     { 
      WebRequest request = base.GetWebRequest(address); 
      if (request is HttpWebRequest) 
      { 
       (request as HttpWebRequest).CookieContainer = _cookieContainer; 
      } 
      return request; 
     } 
} 

,並調用它像這樣

 using(CookieWebClient ck = new CookieWebClient()) 
     { 
      String s = ck.DownloadString(action); 
     } 

甚至嘗試它仍然告訴我,沒有啓用Cookie。

回答

1

亞馬遜有一個API訪問其服務(SOAP)。所以,不要試圖做一些刮擦,我會強烈建議你使用他們的API。

+0

正確的,但具有與雲主機做的,什麼我開發無關與AWS。 –

+0

@ kelton52,他們有SOAP網絡服務:http://flyingpies.wordpress.com/2009/08/01/17/對不起,我在我的答案中發佈了錯誤的鏈接。現在應該修復。 –

+0

這似乎比我需要的方式,再加上我不使用wpf。我甚至沒有嘗試登錄,我只需要亞馬遜意識到我正在使用cookie。 –