2011-03-09 99 views
1

我需要使用用戶名和密碼從安全URL(https)獲取數據。我如何實現這一點。從互聯網上獲取數據

我使用下面的命令提前

li_rc = linet_main.GetURL("http://www.webservicex.net/WeatherForecast.asmx?WSDL", luo_data) 

謝謝, 拉梅什我能夠從一個正常的URL訪問數據。

回答

4

我發現最簡單的方法是通過OLE使用XMLHttp對象。這裏是PB 10.5的一個導出函數,它使用它來打開加密的Google頁面(它的後部分是404,因爲它們的頁面不接受它們,但結果是有效的例子)。

$PBExportHeader$f_test_xmlhttps.srf 
global type f_test_xmlhttps from function_object 
end type 

forward prototypes 
global subroutine f_test_xmlhttps() 
end prototypes 

global subroutine f_test_xmlhttps();//First download and install the latest XMLHttp package 
//(this link goes to the one listed in the connectToNewObject call 
//http://www.microsoft.com/downloads/details.aspx?familyid=3144b72b-b4f2-46da-b4b6-c5d7485f2b42&displaylang=en#filelist 

//XMLHttp object method summary 
//http://msdn.microsoft.com/library/default.asp?url=/library/en-us/xmlsdk/html/xmmscxmldommethods.asp 

String ls_get_url, ls_post_url 
String ls_post_variables, ls_response 
String ls_response_text, ls_status_text 
long ll_status_code 
OleObject loo_xmlhttp 

//include parameters on the URL here for get parameters 
ls_get_url = "https://encrypted.google.com/" 

try 
    //Create an instance of our COM object 
    loo_xmlhttp = CREATE oleobject 
    loo_xmlhttp.ConnectToNewObject("Msxml2.XMLHTTP.4.0") 

    //First lets do a GET request 
    //All request parameters should be included in the URL 
    loo_xmlhttp.open ("GET",ls_get_url, false) 
    loo_xmlhttp.send() 

    //Get our response 
    ls_status_text = loo_xmlhttp.StatusText 
    ll_status_code = loo_xmlhttp.Status 

    //Check HTTP Response code for errors 
    //http://kbs.cs.tu-berlin.de/~jutta/ht/responses.html 
    if ll_status_code >= 300 then 
    MessageBox("HTTP GET Request Failed", ls_response_text) 
    else 
    //Get the response we received from the web server 
    ls_response_text = loo_xmlhttp.ResponseText 

    MessageBox("GET Request Succeeded", ls_response_text) 
    end if 

    //Lets do a POST now, We would pass a String 
    //in the send() call that contains the post data in the 
    //format name1=value1&name2=value2&... 
    ls_post_url = "https://encrypted.google.com/" 
    ls_post_variables = "" 

    loo_xmlhttp.open ("POST",ls_post_url, false) 
    loo_xmlhttp.send(ls_post_variables) 

    //Get our response 
    ls_status_text = loo_xmlhttp.StatusText 
    ll_status_code = loo_xmlhttp.Status 

    //Check HTTP Response code for errors 
    //http://kbs.cs.tu-berlin.de/~jutta/ht/responses.html 
    if ll_status_code >= 300 then 
    MessageBox("HTTP POST Request Failed", ls_response_text) 
    else 
    //Get the response we received from the web server 
    ls_response_text = loo_xmlhttp.ResponseText 

    MessageBox("POST Request Succeeded", ls_response_text) 
    end if 

    //Done so cleanup 
    loo_xmlhttp.DisconnectObject() 

catch (RuntimeError rte) 

    MessageBox("Error", "RuntimeError - " + rte.getMessage()) 

end try 

end subroutine 

保存爲f_test_xmlhttps.srf並導入到PowerBuilder中的pbl。這也適用於HTTP。

+0

謝謝道格曼。對此我還有一個問題。如果我的https網址需要身份驗證(用戶名/密碼)才能打開,那我們該如何傳遞? 「ls_post_variables」(來自你的代碼)有幫助嗎? – Ramesh

+0

@Ramesh:是的,如果您對請求使用POST,那麼您的ls_post_variables變量將包含格式爲'name1 = value1&name2 = value2'的名稱值對。這將模擬POST如果通過Web瀏覽器完成將看起來像什麼。如果您使用的是GET,那麼您只需將它們追加到URL變量ls_get_url的末尾,如'mysite.com?name1 = value1&name2 = value2' –