我正在编写一个 Web 应用程序,可以显示 Sonarqube 的代码气味结果,但我也希望它有时可以创建自定义规则。目前,我可以使用Java中的HTTPClient或Js中的XMLHttpRequest从服务器获取数据。但是,我真的被困在向服务器发送消息上。
在Js中,我尝试过这些代码来登录:(CORS已在我的chrome中禁用)
request.open("POST", "http://localhost:9000/api/authentication/login?login=admin&password=admin", true);
request.send();
响应状态码为
200
,表示成功。但是,当我尝试执行某些需要许可的操作时
request.open("POST", "http://localhost:9000/api/qualityprofiles/copy?fromKey=" + fromKey + "&toName=" + name, true);
request.send();
结果是
401
,未经授权。
经过一番研究,我将代码更改为
var base64encodedData = ("admin:admin").toString('base64');
request.open("POST", "http://localhost:9000/api/qualityprofiles/copy?fromKey=" + fromKey + "&toName=" + name, true);
request.setRequestHeader("Authorization", "Basic " + base64encodedData);
request.send();
回复是
405
,没有找到方法。
经过进一步研究,有人提到
request.withCredentials
应该设置为true
。我添加进去后,又出现了CORS问题。 (禁用 CORS)
(我对 Sonarqube API 有点困惑。我这么说的原因是,在我看来,这个 API 的目的是简化外部使用 Sonarqube 服务器的方法。但是,因为它不允许CORS,这是否意味着我不能在自己的网页上使用该 API?)
由于Js没有运气,所以我转而使用Java。
在Java中,我运行了这个:(我也完成了登录)
HttpPost post = new HttpPost("http://localhost:9000/api/qualityprofiles/copy?fromKey=AV7dToVK_BWPTtE1c9vU&toName=testtt");
try(CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpResponse response = httpClient.execute(post);) {
System.out.println(response.getStatusLine());
}
我得到了:
HTTP/1.1 401
然后我更改我的代码,请按照此链接了解使用 API 进行基本身份验证
CredentialsProvider provider = new BasicCredentialsProvider();
UsernamePasswordCredentials credentials
= new UsernamePasswordCredentials("admin", "admin");
provider.setCredentials(AuthScope.ANY, credentials);
HttpClient client = HttpClientBuilder.create()
.setDefaultCredentialsProvider(provider)
.build();
HttpResponse response = client.execute(
new HttpPost("http://localhost:9000/api/qualityprofiles/copy?fromKey=AV7dToVK_BWPTtE1c9vU&toName=testtt"));
int statusCode = response.getStatusLine().getStatusCode();
System.out.println(statusCode);
再说一遍,
401
总而言之,我的问题是:如何使用Java代码或Js代码(首选)将消息POST到授权的SonarQube服务器?
感谢任何帮助!
更新
我现在正在尝试使用curl,这是我在终端中运行的
curl -X POST -u deb3dd4152c571bcdb7b965e1d99b23a4e5c9505: http://localhost:9000/api/qualityprofiles/copy?fromKey=AV7dToVK_BWPTtE1c9vU&toName=test_file
我得到了这个回复,但我不知道如何
{"errors":[{"msg":"The 'toName' parameter is missing"}]}
CURL 的第二次更新
我跑了:
curl -X POST -F "fromKey=AV7dToVK_BWPTtE1c9vU;toName=test_file" -v -u deb3dd4152c571bcdb7b965e1d99b23a4e5c9505: http://localhost:9000/api/qualityprofiles/copy
结果:
* Trying ::1...
* Connected to localhost (::1) port 9000 (#0)
* Server auth using Basic with user 'deb3dd4152c571bcdb7b965e1d99b23a4e5c9505'
> POST /api/qualityprofiles/copy HTTP/1.1
> Host: localhost:9000
> Authorization: Basic ZGViM2RkNDE1MmM1NzFiY2RiN2I5NjVlMWQ5OWIyM2E0ZTVjOTUwNTo=
> User-Agent: curl/7.49.1
> Accept: */*
> Content-Length: 179
> Expect: 100-continue
> Content-Type: multipart/form-data; boundary=------------------------c22bb5dd76f44ac4
>
< HTTP/1.1 100
< HTTP/1.1 400
< Content-Type: application/json
< Content-Length: 56
< Date: Wed, 04 Oct 2017 00:43:47 GMT
< Connection: close
<
* Closing connection 0
{"errors":[{"msg":"The 'toName' parameter is missing"}]}
对于遇到同样问题的任何人。我已经可以在 Java 代码上运行了,这是我的代码
HttpPost post = new HttpPost("http://localhost:9000/api/qualityprofiles/copy?fromKey=AV7dToVK_BWPTtE1c9vU&toName=testtt");
post.setHeader("Authorization", "Basic ZGViM2RkNDE1MmM1NzFiY2RiN2I5NjVlMWQ5OWIyM2E0ZTVjOTUwNTo=");
try(CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpResponse response = httpClient.execute(post);) {
System.out.println(response.getStatusLine());
}
您可能会注意到我添加了一行设置标题。我之前已经做过类似的事情,将我的登录名和密码编码为这种 base64 格式,但它们都无法正常工作。该工作编码字符串取自 CURL 方法。 (来自我的第二次更新)
我尝试过一些在线base64编码和解码工具,但结果与我从CURL方法得到的结果不符,所以如果你在这方面遇到困难,请运行CURL来获取编码后的标头并将其传入!如果有人可以解释更好的版本,那就太好了!
另外,我仍然对 Js 版本的运行感兴趣。如果您知道答案,请分享!
这是 C# 的答案:
string url = "http://localhost:9000/api/project_badges/quality_gate?project=projectName";
HttpClient client = new HttpClient();
var byteArray = Encoding.ASCII.GetBytes("username:password");
client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray));
string responseString = string.Empty;
var response = client.PostAsync(url, null).Result;
if (response.IsSuccessStatusCode)
{
var responseContent = response.Content;
responseString = responseContent.ReadAsStringAsync().Result;
}