gpt4 book ai didi

javascript - 使用 RHINO js 引擎发起 http 请求

转载 作者:数据小太阳 更新时间:2023-10-29 05:26:19 25 4
gpt4 key购买 nike

我正在尝试使用 Mozilla/Rhino js 引擎在命令行中测试一些 SOAP 请求。但是,用于发出请求的常规对象(XMLHttpRequest、HttpRequest)似乎都不可用。为什么是这样?我可以导入库吗?

最佳答案

我能够通过以下代码使用 Rhino 让它工作。

var post = new org.apache.commons.httpclient.methods.PostMethod("https://someurl/and/path/");
var client = new org.apache.commons.httpclient.HttpClient();

// ---- Authentication ---- //
var creds = new org.apache.commons.httpclient.UsernamePasswordCredentials("username", "password");
client.getParams().setAuthenticationPreemptive(true);
client.getState().setCredentials(org.apache.commons.httpclient.auth.AuthScope.ANY, creds);
// -------------------------- //

post.setRequestHeader("Content-type", "application/xml");
post.setRequestEntity(new org.apache.commons.httpclient.methods.StringRequestEntity(buildXML(), "text/plain", "ASCII" ));

var status = client.executeMethod(post);
var br = new java.io.BufferedReader(new java.io.InputStreamReader(post.getResponseBodyAsStream()));
var response = "";
var line = br.readLine();
while(line != null){
response = response + line;
line = br.readLine();
}

post.releaseConnection();

关于javascript - 使用 RHINO js 引擎发起 http 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22157910/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com