无法使用Javascript作为BSF采样器中的选定语言在Jmeter中发送HTTP Post请求

无法使用Javascript作为BSF采样器中的选定语言在Jmeter中发送HTTP Post请求,javascript,jmeter,Javascript,Jmeter,我试图使用BSF采样器调用HTTP post请求,所选语言是JavaScript。我使用JMTER2.13作为发送请求的工具。 下面是我正在使用的代码片段 var http = new XMLHttpRequest(); var url = "https://localhost/oauth/token"; var params = "client_id=api_client&client_secret=sade1!&response_type=token&

我试图使用BSF采样器调用HTTP post请求,所选语言是JavaScript。我使用JMTER2.13作为发送请求的工具。 下面是我正在使用的代码片段

    var http = new XMLHttpRequest();
    var url = "https://localhost/oauth/token";
    var params = "client_id=api_client&client_secret=sade1!&response_type=token&grant_type=client_credentials";
    http.open("POST", url, true);


    http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

    http.onreadystatechange = function() {
        if(http.status == 200) {
            alert(http.responseText);
        }
    }
但我收到一条响应错误消息,如下所示:

"org.apache.bsf.BSFException: JavaScript Error: Internal Error: org.mozilla.javascript.EcmaError: ReferenceError: "XMLHttpRequest" is not defined."
有没有人能告诉我是否缺少任何导入,或者代码中是否有任何错误

提前谢谢

问候,, Hari是在浏览器中实现的,JavaScript引擎喜欢或不喜欢这个类

出于性能和语言的考虑,等效代码如下:

import org.apache.http.NameValuePair
import org.apache.http.client.entity.UrlEncodedFormEntity
import org.apache.http.client.methods.HttpPost
import org.apache.http.impl.client.HttpClientBuilder
import org.apache.http.message.BasicNameValuePair
import org.apache.http.util.EntityUtils

def client = HttpClientBuilder.create().build()
def post = new HttpPost('https://localhost/oauth/token')

def params = new ArrayList<NameValuePair>()
params.add(new BasicNameValuePair('client_id', 'api_client'))
params.add(new BasicNameValuePair('client_secret', 'sade1'))
params.add(new BasicNameValuePair('response_type','token'))
params.add(new BasicNameValuePair('grant_type','client_credentials'))
post.setEntity(new UrlEncodedFormEntity(params))

def response = client.execute(post)

if (response.getStatusLine().statusCode == 200) {
    log.info(EntityUtils.toString(response.getEntity()))
}
else {
    log.info('Error: ' + response.getStatusLine().getReasonPhrase())
}
import org.apache.http.NameValuePair
导入org.apache.http.client.entity.UrlEncodedFormEntity
导入org.apache.http.client.methods.HttpPost
导入org.apache.http.impl.client.HttpClientBuilder
导入org.apache.http.message.BasicNameValuePair
导入org.apache.http.util.EntityUtils
def client=HttpClientBuilder.create().build()
def post=新的HttpPost('https://localhost/oauth/token')
def params=new ArrayList()
添加(新的BasicNameValuePair('client\u id','api\u client'))
添加(新的BasicNameValuePair('client_secret','sade1'))
添加(新的BasicNameValuePair('response_type','token'))
添加(新的BasicNameValuePair('grant_type','client_credentials'))
post.setEntity(新的UrlEncodedFormEntity(参数))
def response=client.execute(post)
if(response.getStatusLine().statusCode==200){
log.info(EntityUtils.toString(response.getEntity())
}
否则{
log.info('错误:'+response.getStatusLine().getReasonPhrase())
}


还请注意,您可以使用普通采样器实现相同的功能,您可以使用

控制标头,为什么不使用HTTP请求呢?我们已经完成了HTTP请求。但是,即使在本地将HTTP请求头内容类型传递为“application/x-www-form-urlencoded”并将commom HTTP头内容类型传递为“application/Json”,现有脚本也会失败“。我尝试使用HTTP标头管理器并定义了内容类型,但它对我无效。我会试试上面的建议。。!我是否可以将返回的响应保存到变量并将令牌解析为字符串形式的变量?