2011-03-09 6 views
1

私は、ユーザーIDとパスワードで保護されたURL(https)からデータを取得する必要があります。どうすればそれを実装できますか?インターネットからデータを取得する

私は、以下のコマンドを事前に

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

おかげで、 ラメシュを使用して、通常のURLからデータにアクセスすることができています。

答えて

4

最も簡単な方法は、OLE経由でXMLHttpオブジェクトを使用することです。ここには暗号化されたGoogleのページをヒットするためにそれを使用するPB 10.5のエクスポートされた関数があります(そのページは受け入れられていないのでポストの部分にありますが、結果は例として有効です)。

$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

ありがとうDougman。私はこれについてもう一つ質問があります。私のhttps URLに認証(ユーザID /パスワード)が必要な場合、どのようにそれを渡すことができますか?あなたのコードからの "ls_post_variables"は役に立ちますか? – Ramesh

+0

@Ramesh:リクエストでPOSTを使用している場合、ls_post_variables変数には、名前の値のペアが 'name1 = value1&name2 = value2'の形式で格納されます。これは、WebブラウザでPOSTを実行した場合のPOSTの外観をシミュレートします。 GETを使用している場合は、URL変数ls_get_urlの最後に 'mysite.com?name1 = value1&name2 = value2'のように追加するだけです –

関連する問題