Jenkins管道无法执行HttpClient.execute(HttpPost)

时间:2018-03-06 10:31:47

标签: jenkins groovy jenkins-pipeline

我在Jenkins管道中的groovy配置:

HttpUriRequest postRequestLogin = new HttpPost();

List<NameValuePair> urlParameters = new ArrayList<NameValuePair>();
urlParameters.add(new BasicNameValuePair("j_username", "${user}"))
urlParameters.add(new BasicNameValuePair("j_password", "${pass}"))
postRequestLogin.setEntity(new UrlEncodedFormEntity(urlParameters))

CloseableHttpResponse responseLogin = httpClient.execute(postRequestLogin)

我收到以下错误:

  

没有方法签名:   org.apache.http.impl.client.InternalHttpClient.execute()适用   对于参数类型:(org.apache.http.client.methods.HttpPost)值:   [org.apache.http.client.methods.HttpPost@eb47360]可能的解决方案:   执行(org.apache.http.client.methods.HttpUriRequest),   执行(org.apache.http.client.methods.HttpUriRequest,   org.apache.http.client.ResponseHandler)   execute(org.apache.http.HttpHost,org.apache.http.HttpRequest),   执行(org.apache.http.client.methods.HttpUriRequest,   org.apache.http.protocol.HttpContext)   execute(org.apache.http.HttpHost,org.apache.http.HttpRequest,   org.apache.http.client.ResponseHandler)   执行(org.apache.http.client.methods.HttpUriRequest,   org.apache.http.client.ResponseHandler,   org.apache.http.protocol.HttpContext)

1 个答案:

答案 0 :(得分:1)

提出请求的其他方式:

    URL url = url_string.toURL()
    // Create authorization header format using Base64 encoding
    String userpass = username + ":" + apiKey;
    String basicAuth = "Basic " + javax.xml.bind.DatatypeConverter.printBase64Binary(userpass.getBytes());
    // Open connection
    URLConnection connection = url.openConnection()

    connection.setRequestProperty ("Authorization", basicAuthString())
    connection.setRequestMethod("POST")
    connection.doOutput = false;

    // Open input stream
    InputStream inputStream = connection.getInputStream()
    // Close the stream
    inputStream.close()

    return connection.getResponseCode()