2016-05-12 63 views
1

好了,所以這是一個複雜的問題。已經過了兩天半的研究和調試,我現在在這裏。如何做兩個POST請求在一個會話(JSESSIONID)/餅乾

目標: 從政府GST/HST查找網站獲取數據。由於它是舊的(作爲Java小程序使用)並且沒有REST API支持,所以我必須手動對網站執行2個Web請求。 (經過調試的整個一天,我發現,我不得不開始搜索,因爲,接受存儲在cookie之前先接受條款和conds鏈接的網址爲:Government Search Website

問題遇到: Flowgear的Web請求節點不允許您指定「Cookie」標頭,因此我無法告訴它在兩個單獨的請求中使用特定的Cookie。所以我必須這樣做:Workflow

注意: 我想在兩個POST請求上使用相同的Cookie,所以我得到適當的數據返回該特定的會話。

當前問題: 我收到此錯誤:「腳本執行錯誤:無法發送與此謂詞類型的內容主體。」

代碼:

using System; 
using System.IO; 
using System.Collections.Generic; 
using System.Xml; 
using System.Linq; 
using System.Xml.Linq; 
using System.Net; 
using System.Text; 

namespace CSharpScript 
{ 
    public partial class Processor 
    { 
    public object Process() 
    { 
     var request = (HttpWebRequest)WebRequest.Create("https://www.businessregistration-inscriptionentreprise.gc.ca/ebci/brom/registry/registryservlet"); 
     var postData = "iagree=yes"; 
     var data = Encoding.ASCII.GetBytes(postData); 

     request.Method = "POST"; 
     request.ContentType = "application/x-www-form-urlencoded"; 
     request.ContentLength = data.Length; 
     request.KeepAlive = true; 
     request.Host = "www.businessregistration-inscriptionentreprise.gc.ca"; 
     Uri target = new Uri("https://www.businessregistration-inscriptionentreprise.gc.ca/ebci/brom/registry/registryservlet"); 
     CookieContainer reqCookies = new CookieContainer(); 
     request.CookieContainer = reqCookies; 
     request.CookieContainer.Add(new Cookie("id","YxmlT6nC0FPUakMqOF7S5ZGM-URCkTR-sjddOY02Gz2XfOodrOia!1465666898"){ Domain = target.Host }); 
     //request.Headers["Cookie"] = "YxmlT6nC0FPUakMqOF7S5ZGM-URCkTR-sjddOY02Gz2XfOodrOia!1465666898"; 
     using (var stream = request.GetRequestStream()) 
     { 
      stream.Write(data, 0, data.Length); 
     } 

     var response = (HttpWebResponse)request.GetResponse(); 
     var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd(); 
     //Part 2 
     var request2 = (HttpWebRequest)WebRequest.Create("https://www.businessregistration-inscriptionentreprise.gc.ca/ebci/brom/registry/registryPromptSubmit.do"); 
     var postData2 = "businessName=COMPANYNAMEASSTRINGGOESHERE"; 
     postData2 += "&businessNumber=SOMENUMBERGOESHERE"; 
     postData2 += "&requestDate=2014-09-02"; 
     var data2 = Encoding.ASCII.GetBytes(postData2); 

     request2.Method = "POST"; 
     request2.ContentType = "application/x-www-form-urlencoded"; 
     request2.ContentLength = data2.Length; 
     request2.KeepAlive = true; 
     request2.Host = "www.businessregistration-inscriptionentreprise.gc.ca"; 
     //Uri target = new Uri("https://www.businessregistration-inscriptionentreprise.gc.ca/ebci/brom/registry/registryservlet"); 
     request2.CookieContainer = reqCookies; 
     //request.Headers["Cookie"] = "YxmlT6nC0FPUakMqOF7S5ZGM-URCkTR-sjddOY02Gz2XfOodrOia!1465666898"; 
     using (var stream = request.GetRequestStream()) 
     { 
      stream.Write(data2, 0, data2.Length); 
     } 

     var response2 = (HttpWebResponse)request2.GetResponse(); 

     responseString = new StreamReader(response2.GetResponseStream()).ReadToEnd(); 
     return responseString; 
    } 
} 

}

調試: 如果我除了return語句下的 '第2部分' 刪除一切,第一個POST正常工作。 在此之前,我在自己的腳本節點有2個部分,併成功返回的數據,但搜索結果中失蹤。 (由於會話信息/ cookie的信息不結轉)

重要提示:我已經做郵差這些確切的兩個請求和它的作品成功地在那裏。

如何提交數據POST請求到這個網站通過flowgear任何想法,將不勝感激。 :(

更新:對於第二個POST請求我才意識到自己忘了改

using (var stream = request.GetRequestStream()) 

相匹配的響應對象「請求2」:

using (var stream = request2.GetRequestStream()) 

的Web請求成功現在的工作解決!

但是,我的問題仍然存在:我如何使兩個單獨的請求共享cookie或會話,以及如何將Connection標題屬性設置爲'Keep-alive'?

回答

0

嘗試創建的Web請求的連接,並提供會話密鑰屬性的值。然後在兩個請求上使用相同的連接,這會導致它們共享同一個cookie jar。您不需要手動嘗試注入標題。

請注意,Cookie jar的作用域爲Flowgear站點,並且位於內存中,因此您可以在不同的工作流程中擁有這兩個請求。例如。一個在父母,一個在子工作流程中。

+0

我似乎無法找到Web Request節點中的sessionKey屬性,並且您是什麼意思在兩個請求上使用相同的連接?在爲連接屬性建立一個新的連接並設置爲同一個? – ykadaru

+0

該屬性位於連接而不是節點本身。看看http://developers.flowgear.net/kb/Node:Web_Request – Daniel

+0

啊好吧!謝謝,這有很大幫助。 – ykadaru