2013-06-22 159 views
1

我正在調用外部API。令人討厭的是,它返回的數據位於標題中(文本響應爲空)。如何在Http請求中訪問響應頭

如何訪問響應的標題?

這就是我想:

Dim httpRequest, postResponse 
    Set httpRequest = Server.CreateObject("MSXML2.ServerXMLHTTP") 
    httpRequest.Open "POST", "http://www.api.com", False, "un", "pw" 
    httpRequest.SetRequestHeader "Content-Type", "application/json" 
    httpRequest.setRequestHeader "Content-Length", len(jsondata) 
    httpRequest.Send data 
    if httpRequest.status = 200 then 
     response.write httpRequest.getResponseHeader 
     response.write httpRequest.ResponseText 
    end if 
    Set httpRequest = nothing 

但它給我:

msxml3.dll error '80072f76' 

    The requested header was not found 

和獎金的問題:我只注意到 「MSXML2.ServerXMLHTTP」 的 「XML」 部分 - 我使用正確的協議嗎?它一直用於直接的帖子,直到現在。

+0

是不是你缺少你想要檢索哪個頭?又名'httpRequest.getResponseheader(「服務器」)'[如圖所示](http://msdn.microsoft.com/en-us/library/windows/desktop/ms765415(v = vs.85).aspx) – rene

回答

2

您需要指定要檢索的response header名稱:

response.write httpRequest.getResponseHeader("SomeHeaderName") 

有沒有隻有一個響應頭。可能有很多。你有標準的響應標題,如Content-Type,你也可以有自定義標題。

和獎金的問題:我只注意到的 「MSXML2.ServerXMLHTTP」中的「XML」的一部分 - 我使用權協議?

是的,絕對的,這是從傳統的ASP應用程序用來發送HTTP請求的正確的COM對象。

+0

謝謝Darin,那只是我正在尋找的答案 - 非常感謝 –