我正在尝试通过发送html标头在SAS和Web之间建立通信,因此能够通过http / https使用GET / POST进行通信。
我正在尝试使用以下代码使用Curl进行连接:
filename curl pipe 'c:\curl\curl http://support.sas.com" lrecl=8192;
当我使用家庭互联网连接执行它时,此代码段运行完全正常。
但是,当我尝试使用我的worplace互联网执行相同操作时,会出现错误:未找到主机。
我明白,当我在我的工作场所运行它时,它假定support.sas.com是本地局域网的一部分,因此出错。但是如何到达实际网站并在sas日志中获取源代码。
data _null_;
infile curl;
input;
file log;
put _infile_;
run;
当我使用文件名套接字时,我遇到了同样的问题。
请建议。
由于 阿布舍克巴克
答案 0 :(得分:1)
实现这一目标是可能的,但不幸的是,并非如此简单。下面是我用来做这种事情的宏。我建议阅读标题中链接的白皮书。
一旦您了解了他们在白皮书中所做的工作,下面的代码应该更有意义。看看我在宏下面提供的两个例子。第一个示例只是访问常规HTTPS网页。第二个例子我用来提取需要通过网页上的用户/密码表单登录的数据。我在第二个例子中明显改变了域名,页面名称等,所以它不适合你,但它应该给你一个起点。
我还应该补充一点,我还使用了CURL从Web服务中提取数据,这些数据允许我在请求URL中指定用户/传递。 AFAIK如果您无法在请求网址中指定登录详细信息,那么唯一的方法是使用以下技术。
最后,根据@Laurent de Walick的说法,这也要求您整理代理设置。
/*****************************************************************************
** PROGRAM: CMN_MAC.HTTP.SAS
**
** PERFORMS A HTTP REQUEST AND SAVES THE RESULT TO A FILE.
** BASED ON WHITEPAPER FOUND AT: http://www2.sas.com/proceedings/sugi30/100-30.pdf
**
** PARAMETERS: SEE http://www2.sas.com/proceedings/sugi30/100-30.pdf
**
******************************************************************************
** HISTORY:
** 1.0 MODIFIED: 7-MAY-2014 BY:RP
** - CREATED
*****************************************************************************/
%macro http(http=%nrstr(), save_file=, hostname=, delimiter=%str(|),fileref=browser, termstr=CRLF);
%local str1 str2 pos http;
%let str1=;
%let str2=;
%do %while (%index(&http,&delimiter)>0);
%let pos=%index(&http,&delimiter);
%let str1=&str1%qsubstr(&http, 1, &pos-1)/;
%let str2=&str2%qsubstr(&http, 1, &pos-1)'0d0a'x;
%let http=%qsubstr(&http, &pos+1, %length(&http)-&pos);
%end;
filename &fileref socket "&hostname:80" termstr=&termstr lrecl=8192;
data _null_;
retain chunked;
length rec2 $10000 chunked $1;
infile &fileref length=len;
if _n_=1 then do;
file &fileref;
put %unquote(&str1);
file &save_file;
put '>>> HTTP Request:' '0d0a'x;
put %unquote(&str2);
put '0d0a'x '<<< HTTP Response:' '0d0a'x;
end;
file &save_file recfm=n;
input record $varying8192. len;
put record $varying8192. len '0d0a'x;
if record='Transfer-Encoding: chunked' then chunked='Y';
if len=0 and chunked='Y' then do while (1); %*** look for & process chunks;
input chunksize $varying8. len / record $varying8192. len;
if len ne 0 then do;
l2=len+countc(record, '0a'x);
rec2=tranwrd(record, '0a'x, '0d0a'x);
put rec2 $varying10000. l2;
end;
else stop; *** found null record at end of file, end loop;
end;
run;
%mend;
一个简单的例子:
%http(
hostname=support.sas.com,
save_file='c:\support_sas.txt',
http=%nrstr(
'GET / HTTP/1.1' |
'Host: support.sas.com' |
'Connection: Close' | )
);
一个更复杂的例子:
/*
** INSTRUCTIONS:
** 1) IN FIREFOX, INSTALL THE LIVEHTTPHEADERS PLUG IN
** 2) ONCE ITS ENABLED, GO INTO THE OPTIONS AND MAKE SURE ITS TURNED ON IN THE SIDEBAR
** 3) MAKE SURE THE SIDE-BAR IS VISIBLE VIEW->OPTIONS->SIDEBAR
** 4) OPEN A PRIVATE BROWSER WINDOW.
** 5) PASTE IN THE URL. ENTER THE SIGNON DETAILS AND GET TO THE PAGE YOU DESIRE
** 6) COPY/PASTE THE POST REQUEST THAT WAS MADE FROM THE SIDEBAR.
** 7) IF THE POST REQUEST HAS A LINE THAT SAYS ACCEPT-ENCODING GZIP THEN DELETE IT
** 8) MAKE SURE THAT YOU MOVE THE REQUEST PARAMETERS FROM THE VERY FIRST LINE TO THE LINE UNDER THE
** CONTENT LENGTH. ADJUST THE CONTENT LENGTH ACCORDINGLY.
** 9) MAKE SURE YOU CHANGE THE CONNECTION TO CLOSE RATHER THAN KEEP ALIVE.
** 10) I THINK THATS IT.
**
** EXAMPLE:
**
**
** URL I WANTED TO RETRIEVE THAT WAS BEHIND XXX LOGIN PAGE:
**
** http://BLAH_tools.someplace.com/soap.php?id=9fb7692e120e5a7fc6ad1af44b
**
** THE STUFF I COPIED AND PASTED FROM THE LIVEHTTPHEADERS SIDEBAR:
**
** POST /soap.php?id=9fb7692e120e5a7fc6ad1af44b HTTP/1.1
** Host: BLAH_tools.someplace.com
** User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:29.0) Gecko/20100101 Firefox/29.0
** Accept: text/html,application/xhtml+xml,application/xml;q=0.9,* / *;q=0.8
** Accept-Language: en-US,en;q=0.5
** Accept-Encoding: gzip, deflate
** Referer: http://live.x.com/?v=2&r=ovL29scF90b29scy5qdWJpbGVlLnRzcy9hcHBsaWNhdGlvbi9zb2FwX3Bvc3GhwP3VuaXF1ZV9pZD0yYTMxOWZiNzY5MmUxMjBlNWE3ZmM2YWQxYWY0NGIxZiZtb2RlPXNs
** Cookie: XXX_key=1e43eeaec0fbc7e506579c
** Connection: keep-alive
** Cache-Control: max-age=0
** Content-Type: application/x-www-form-urlencoded
** Content-Length: 46
** key=7e1e43eeaec0fbc7e506579ce7fc8edd&postdata=
**
** THE CALL TO THE MACRO AFTER CHANGES HAD BEEN MADE TO THE HEADERS:
*/
%http(hostname = BLAH_tools.someplace.com,
save_file = 'c:\xxx.txt',
http = %nrstr(
'POST /application/soap_post.php HTTP/1.1' |
'Host: BLAH_tools.someplace.com' |
'User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:29.0) Gecko/20100101 Firefox/29.0' |
'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,* / *;q=0.8' |
'Accept-Language: en-US,en;q=0.5' |
'Referer: http://live.x.com/?v=2&r=ovL29scF90b29scy5qdWJpbGVlLnRzcy9hcHBsaWNhdGlvbi9zb2FwX3Bvc3GhwP3VuaXF1ZV9pZD0yYTMxOWZiNzY5MmUxMjBlNWE3ZmM2YWQxYWY0NGIxZiZtb2RlPXNs' |
'Cookie: XXX_key=1e43eeaec0fbc7e506579c' |
'Connection: close' |
'Cache-Control: max-age=0' |
'Content-Type: application/x-www-form-urlencoded' |
'Content-Length: 100' |
|
'id=9fb7692e120e5a7fc6ad1af44b&key=1e43eeaec0fbc7e506579c&postdata=' |
)
);
**
** FINALLY - NOTE THAT TO PARAMETERIZE THE REQUEST VARIABLES MY FINAL LINE LOOKED LIKE THIS:
*;
"id=&iMyID%nrstr(%bquote(&)key=1e43eeaec0fbc7e506579c%bquote(&)postdata=)" |