Java 如何使用基本身份验证不安全地发布到RESTful服务器?
我如何做与此等效的工作:Java 如何使用基本身份验证不安全地发布到RESTful服务器?,java,rest,Java,Rest,我如何做与此等效的工作: curl \ --insecure \ --request POST \ --header "<header>" \ --data "<Complex JSON Object>" \ "https://<username>:<password>@<URL>?<params>" curl\ --不安全\ --请帖\ --标题“”\ --数据“”\ “https:/:@?” 在爪
curl \
--insecure \
--request POST \
--header "<header>" \
--data "<Complex JSON Object>" \
"https://<username>:<password>@<URL>?<params>"
curl\
--不安全\
--请帖\
--标题“”\
--数据“”\
“https:/:@?”
在爪哇
身份验证很奇怪,只有在使用--unsecure
标志和基本身份验证时才有效
我尝试过各种库,但都无法使用。有大量http客户端库。其中最流行的是ApacheCommonsHTTP客户端。您可以使用它发布JSON,如下所示:
CloseableHttpClient client = HttpClients.createDefault();
HttpPost httpPost = new HttpPost("http://www.example.com");
String json = "{"id":1,"name":"John"}";
StringEntity entity = new StringEntity(json);
httpPost.setEntity(entity);
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Content-type", "application/json");
CloseableHttpResponse response = client.execute(httpPost);
assertThat(response.getStatusLine().getStatusCode(), equalTo(200));
client.close();
-k
/--unsecure
被记录为“允许连接到没有证书的SSL站点(H)”;但您的站点不是SSL站点,所以该标志不应该有任何影响。你确定你的命令是你想要的吗?是的。我知道它的作用。我发这封信的时候忘了“s”。谢谢你指出。好的。那么,您的问题是您不知道如何实现与--不安全,或者您不知道如何实现基本身份验证,还是@ruakh我不知道如何实现与--unsecure
.Process Process=Runtime.getRuntime().exec(“curl-k-X POST--header\”内容类型:application/json\“-d'+body+”);process.waitFor();process.destroy();我需要它有-k(--unsecure)标志。它只返回:线程“main”javax.net.ssl.SSLHandshakeException:sun.security.validator.ValidatorException:PKIX路径生成失败:sun.security.provider.certpath.SunCertPathBuilderException:找不到请求目标的有效认证路径