2014-11-17 68 views
2
xdmp:http-get($url, 
    <options xmlns="xdmp:document-get"> 
     <format>binary</format> 
    </options>)[2] 

大家好,請問xdmp:http-get()支持代理嗎?

上面的查詢不返回從代理服務器的響應。我知道IP地址和端口號以獲得代理服務器中的響應。有誰知道在哪裏添加IP和端口號?

MarkLogic版本:7.x的

最近,我試圖在http://markmail.org/message/sbfj44jtmpsyopyh與下面的代碼討論配置代理。

let $proxy := "http://171.21.15.60:3128" 
let $uri := "http://www.austlii.edu.au/cgi-bin/sinosrch.cgi?results=200;query=damage" 
let $host := tokenize($uri,'/')[3] 
let $proxyuri := concat($proxy, '/', tokenize($uri, '/')[last()]) 
return xdmp:http-post(
    $proxyuri, 
    <options xmlns="xdmp:http"> 
    <headers> 
     <Host>{$host}</Host>   
    </headers> 
    </options> 
) 

但是我收到了一個錯誤的請求作爲迴應。

<response xmlns="xdmp:http"> 
<code>400</code> 
<message>Bad Request</message> 
<headers> 
    <server>squid/3.1.4</server> 
    <mime-version>1.0</mime-version> 
    <date>Thu, 20 Nov 2014 04:09:50 GMT</date> 
    <content-type>text/html</content-type> 
    <content-length>3071</content-length> 
    <x-squid-error>ERR_INVALID_URL 0</x-squid-error> 
    <vary>Accept-Language</vary> 
    <content-language>en</content-language> 
    <x-cache>MISS from l076ddms1</x-cache> 
    <x-cache-lookup>NONE from l076ddms1:3128</x-cache-lookup> 
    <via>1.0 l076ddms1 (squid/3.1.4)</via> 
    <proxy-connection>close</proxy-connection> 
</headers> 
</response> 

看看下面的錯誤

以下錯誤時遇到試圖檢索網址:/sinosrch.cgi?results=200;query=damage

誰能幫助我解決這個問題?

謝謝。

大家好,

我仍然得到同樣的迴應,我曾試圖通過@mblakele告訴步驟之後。

declare namespace http="xdmp:http"; 

declare function local:http-options(
    $options as element(http:options)?, 
    $extra as element(http:options)?) 
as element()? 
{ 
    if (empty($extra)) then $options 
    else if (empty($options)) then $extra 
    else element http:options { 
    (: TODO - needs to handle conflicting options. :) 
    $options/*, 
    $extra/* } 
}; 

declare function local:http-get(
    $proxy as xs:string, 
    $uri as xs:string, 
    $options as element(http:options)?) 
as item()+ 
{ 

    let $uri-toks := tokenize($uri, '/+') 
    let $uri-host := $uri-toks[2] 
    let $options := local:http-options(
    $options, 
    element http:options { 
     element http:headers { 
     element http:host { $uri-host } } }) 
    (: TODO support TLS proxies using https. :) 
    let $uri-proxy := concat(
    'http://', $proxy, 
    substring-after($uri, $uri-host)) 
    return xdmp:http-get($uri-proxy, $options) 
}; 

local:http-get(
    '171.21.15.60:3128', 'http://www.austlii.edu.au/cgi-bin/sinosrch.cgi?results=200;query=damage',()) 

上述代碼的$ URI代理的值:

http://171.21.15.60:3128/cgi-bin/sinosrch.cgi?results=200;query=damage 

上述代碼的$ URI主機的值是:

www.austlii.edu.au 

$的值以上代碼中的選項爲:

<http:options xmlns:http="xdmp:http">  
<http:headers>  
<http:host>www.austlii.edu.au</http:host> 
</http:headers></http:options> 

錯誤是

試圖檢索URL時遇到以下錯誤:/cgi-bin/sinosrch.cgi?results=200;query=damage

回答

0

由於我無法處理代理MarkLogic。我開發了REST API使用.NET通過代理隧道訪問外部網站,我讓MarkLogic調用我的本地web服務。

Hope MarkLogic http-get()將在未來支持代理。

感謝dev的寶貴建議。

1

我不認爲有任何的直接支持,但http://markmail.org/message/sbfj44jtmpsyopyh可能的幫助。

[編輯]由於該代碼有一些問題,這裏是一個簡單的重寫。這仍然不是完全通用的,但它可能更容易調試和增強。

declare namespace http="xdmp:http" ; 

declare function local:http-options(
    $options as element(http:options)?, 
    $extra as element(http:options)?) 
as element()? 
{ 
    if (empty($extra)) then $options 
    else if (empty($options)) then $extra 
    else element http:options { 
    (: TODO - needs to handle conflicting options. :) 
    $options/*, 
    $extra/* } 
}; 

declare function local:http-get(
    $proxy as xs:string, 
    $uri as xs:string, 
    $options as element(http:options)?) 
as item()+ 
{ 
    let $_ := (
    if (matches($proxy, '^\w+(:\d+)?$')) then() 
    else error(
    (), 'BADPROXY', 
     ('Must be a string host:port', xdmp:describe($proxy)))) 
    let $uri-toks := tokenize($uri, '/+') 
    let $uri-host := $uri-toks[2] 
    let $options := local:http-options(
    $options, 
    element http:options { 
     element http:headers { 
     element http:host { $uri-host } } }) 
    (: TODO support TLS proxies using https. :) 
    let $uri-proxy := concat(
    'http://', $proxy, 
    substring-after($uri, $uri-host)) 
    return xdmp:http-get($uri-proxy, $options) 
}; 

local:http-get(
    'localhost:8118', 'http://www.google.com/',()) 
+0

我檢查了這個[鏈接](http://markmail.org/message/sbfj44jtmpsyopyh),但是我收到了ML數據庫 – Gowtham

+0

中的錯誤請求作爲響應(如發佈的問題)嘗試一些調試。將查詢修改爲'return($ host,$ proxyuri)',你應該看到'tokenize($ uri,'/')[last()]'太粗糙了。它切斷了路徑的'cgi-bin'部分。相反,新的URI應該包含主機之後的所有內容。見上面我的編輯。 – mblakele

+0

我仍然收到一個不好的請求作爲迴應。匹配($ proxy,'^ \ w +(:\ d +)?$')這不會對我有用。因爲localhost在我的情況下是ip,它不會匹配你寫的正則表達式。所以我刪除了那部分並試了一下。我在我的問題中發佈了$ uri-proxy,$ uri-host,$ options的值。幫助我如何解決這個問題 – Gowtham