2013-10-29 80 views
0

即時通訊嘗試發送POST請求使用HttpWebRequest和fiddler顯示我即時通訊發送GET? 任何幫助將不勝感激,因爲我能夠看到我做錯了什麼。HttpWebRequest只發送獲取請求,不發帖

代碼:

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(LOGIN_API_BASE_URL); 
     string postString = string.Format("api_email={0}&api_password={1}", EMAIL, PASSWORD); 
     ASCIIEncoding encoding = new ASCIIEncoding(); 
     byte[] data = encoding.GetBytes(postString); 
     CookieContainer cookies = new CookieContainer(); 
     request.CookieContainer = cookies; 
     //request.AllowWriteStreamBuffering = true; 
     request.Method = "POST"; 
     request.ContentType = "application/x-www-form-urlencoded"; 
     request.ContentLength = data.Length; 
     request.Timeout = i_timeout; 
     //request.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.1) Gecko/2008070208 Firefox/3.0.1"; 
     //request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"; 
     //request.Referer = "https://accounts.craigslist.org"; 

     using (Stream writer = request.GetRequestStream()) 
     { 
      writer.Write(data, 0, data.Length); 
      //writer.Close(); 
     } 

     StreamReader responseReader = new StreamReader(request.GetResponse().GetResponseStream()); 
     string responseData = responseReader.ReadToEnd(); 

     responseReader.Close(); 
     request.GetResponse().Close(); 
+0

您要發送密碼爲純文本? – Arran

+0

@Arran im根據API指令工作,並試圖以純文本和字節的形式發送它,但這不是問題 – Liran

回答

0

確定發現問題,鏈接IM發送的請求是http://domain.com,當我將其發送到http://www.domain.com它的工作原理和發個帖子。這個新問題爲什麼是這樣? (答案將得到我的問題的答案,因爲我不喜歡投票我自己的想法)

+1

聽起來像服務器上的URL重寫可能會重定向到www.domain.com的規範形式。計劃不當的URL重寫通常會忘記考慮POST請求,因此重定向會將其轉換爲GET請求。 – Chris

+0

@Chris確定你生病了。但是當我嘗試使用http://www.requestmaker.com/來測試它的請求發送正常和響應也沒關係也返回與http 302(多數民衆贊成在罰款因爲我期待只有cookie中的響應),但在我的代碼即使後仍然即時得到404錯誤....再次感謝您的幫助 – Liran

+2

根據問題的網站,這可能仍然是這種情況。例如,如果使用ASP.NET MVC,並且domain.com/home/dostuff中定義的動作僅爲POST,則嘗試進行GET請求將導致404錯誤,因爲沒有端點接受GET請求。 – Chris