2011-03-09 98 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' –