2014-03-28 108 views
0

我可以在一個網站中使用「發佈」請求,但有一個網站,其中的POST方法似乎沒有工作。我已經失去了主意。 :-(XMLHTTP「發佈」不工作(VBA)

這裏是我的網站測試代碼:

Sub test() 
    Dim result As String 
    Dim myURL As String, postData As String 
    Dim winHttpReq As Object 
    Set winHttpReq = CreateObject("MSXML2.XMLHTTP") 
    Dim ele 
    Dim html As Object 

    myURL = "http://www.mca.gov.in/DCAPortalWeb/dca/CompanyMaster.do?method=getName" 
    postData = "taskID=9412&method=find&cmpnyname=&cmpnyID=U24232TN2004PLC054527" 

    winHttpReq.Open "POST", myURL, False 
    winHttpReq.setRequestHeader "Content-Type", "application/x-www-form-urlencoded" 
    winHttpReq.send (postData) 

    result = winHttpReq.responseText 
    Sheets("sheet1").Range("A1") = result 

    end sub 

該網站的主頁是「http://www.mca.gov.in/DCAPortalWeb/dca/MyMCALogin.do?method=setDefaultProperty&mode=31」我以前livehttp頭以獲取POST URL鏈接

但似乎沒有工作我使用公司ID進行搜索 樣本ID:U24232TN2004PLC054527

不需要你的幫助我認爲我在這裏使用了不正確的URL但是我使用了所有在實時HTTP中顯示的URL頭像和似乎沒有工作。請幫忙!

回答

1

該問題可以使用Fiddler進行分析,它比livehttp標頭提供更多的細節。使用Fiddler composer功能,您可以嘗試POST請求並查看響應。

我對提供的URL感到滿意,並且發現由於未在請求中設置JSESSIONID cookie,響應會返回一個消息「您的會話已過期」。

張貼的要求:

POST http://www.mca.gov.in/DCAPortalWeb/dca/CompanyMaster.do HTTP/1.1 
Content-Type: application/x-www-form-urlencoded 
Host: www.mca.gov.in 
Content-Length: 64 

taskID=9412&method=find&cmpnyname=&cmpnyID=U24232TN2004PLC054527 

的迴應:

HTTP/1.1 200 OK 
Date: Thu, 08 Jan 2015 11:31:55 GMT 
Server: IBM_HTTP_Server 
Surrogate-Control: no-store 
Expires: Thu, 01 Dec 1994 16:00:00 GMT 
Cache-Control: no-cache="set-cookie, set-cookie2" 
X-UA-Compatible: IE=edge 
Vary: Accept-Encoding 
Content-Type: text/html;charset=ISO-8859-1 
Content-Language: en-US 
via: HTTP/1.1 proxy226 
Proxy-Connection: Keep-Alive 
Connection: Keep-Alive 
Set-Cookie: JSESSIONID=0000NHNQgGMo247hf4dpRRcsrLP:17ufavb50; Path=/ 
Content-Length: 702 

在響應主體,我們有消息 「您的會話已過期」。

如果您第一次進入主頁,將設置一個cookie,並將其傳遞給進一步的調用。當你直接提出POST請求時,JSESSIONID cookie沒有設置。

您需要首先在您的VBA代碼中打開主頁,然後閱讀響應標頭以識別cookie,然後使用它填充POST請求標頭。還要注意,POST必須在不帶?method = getName的URL「http://www.mca.gov.in/DCAPortalWeb/dca/CompanyMaster.do」上完成。還需要使用responsebody來創建填充excel文件的數據。

示例代碼:

Sub test_new() 
    Dim result As String 
    Dim myURL As String, postData As String 
    Dim Cookie As String 
    Dim winHttpReq As Object 
    Dim oStream As Object 
    Dim objStream As Object 
    Set winHttpReq = CreateObject("MSXML2.XMLHTTP") 

' GET the JSESSIONID cookie first frm home page 
    myURL = "http://www.mca.gov.in/DCAPortalWeb/dca/MyMCALogin.do?method=setDefaultProperty&mode=31" 
    winHttpReq.Open "GET", myURL, False 
    winHttpReq.send 

' Get the JSESSIONID from cookies ''' JSESSIONID=0000QSz0qJtUmQ8QRmEGBbVBFsm:18ungce99; Path=/ 
    Cookie = Split(winHttpReq.getResponseHeader("Set-Cookie"), ";")(0) 
    myURL = "http://www.mca.gov.in/DCAPortalWeb/dca/CompanyMaster.do"  ' ?method=getName" 
    postData = "taskID=9412&method=find&cmpnyname=&cmpnyID=U24232TN2004PLC054527" 
    winHttpReq.Open "POST", myURL, False 
    winHttpReq.setRequestHeader "Content-Type", "application/x-www-form-urlencoded" 
' POST the request with cookies 
    winHttpReq.setRequestHeader "Cookie", Cookie 
    winHttpReq.send (postData) 

' Get Text from response Body 
    If winHttpReq.Status = 200 Then 
     Set oStream = CreateObject("ADODB.Stream") 
     oStream.Open 
     oStream.Type = 1 
     oStream.Write winHttpReq.responseBody 
     oStream.Position = 0 
     oStream.Type = 2 'Text 
     oStream.Charset = "UTF-8" 
     result = oStream.ReadText 
    End If 
    Sheets("Sheet1").Range("A1") = result 
End Sub 

注意這僅保存的HTML代碼到單元格A1。如果您確實想使用html內容,則應使用htmlfile對象或將流保存到本地文件中,並使用oStream.SaveToFile,並使用它代替oStream.Position = 0中的代碼。