我有一个包含以下详细信息的外部 REST 资源:
abc.com/orders域名为https)我为此使用以下 Java 代码:
try {
Client client = Client.create();
WebResource webResource = client.resource("abc.com/orders");
ClientResponse response = webResource.header("user", "abcd").accept("application/json")
.get(ClientResponse.class);
if (response.getStatus() != 200) {
throw new RuntimeException("Failed : HTTP error code : " + response.getStatus());
}
String output = response.getEntity(String.class);
System.out.println("Output from Server .... \n");
System.out.println(output);
} catch (Exception e) {
e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
但是我得到了低于异常的结果,尽管它可以正常工作 PostMan
com.sun.jersey.api.client.ClientHandlerException: javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable …Run Code Online (Sandbox Code Playgroud) 我正在开发使用 microprofile Rest 客户端的应用程序。该 REST 客户端应发送带有各种 http 标头的 REST 请求。某些标头名称会动态更改。我的微配置文件休息客户端应该是通用的,但我没有找到如何实现这种行为。根据文档,您需要通过注释指定实现中的所有标头名称,但这不是通用的。有什么方法可以“破解”它并以编程方式添加 HTTP 标头吗?
提前致谢
GenericRestClient genericRestClient = null;
Map<String, Object> appConfig = context.appConfigs();
String baseUrl = (String) appConfig.get("restClient.baseUrl");
path = (String) appConfig.get("restClient.path");
try {
genericRestClient = RestClientBuilder.newBuilder()
.baseUri(new URI(baseUrl)).build(GenericRestClient.class);
}catch(URISyntaxException e){
logger.error("",e);
throw e;
}
Response response = genericRestClient.sendMessage(path, value);
logger.info("Status: "+response.getStatus());
logger.info("Response body: "+response.getEntity().toString());
Run Code Online (Sandbox Code Playgroud)
通用休息客户端代码:
@RegisterRestClient
public interface GenericRestClient {
@POST
@Path("{path}")
@Produces("application/json")
@Consumes("application/json")
public Response sendMessage(<here should go any map of custom headers>, @PathParam("path") String pathParam, String jsonBody);
}
Run Code Online (Sandbox Code Playgroud) 我们可以使用RestClient库(https://github.com/archiloque/rest-client)对Ruby中的资源发出异步POST请求吗?
这个想法是REST Client可以配置REST服务器列表.因此,服务器列表将以循环方式在REST客户端上轮换.
例如REST客户端应用程序 我将配置服务器列表(REST_SERVER1,REST_SERVER2,REST_SERVER3)
1 request -> REST_SERVER1
2 request -> REST_SERVER2
3 request -> REST_SERVER3
4 request -> REST_SERVER1
Run Code Online (Sandbox Code Playgroud)
我搜索了很多,我找不到支持此功能的正确框架.
我正在尝试使用RestClient来检索使用SSL客户端证书保护的页面.我的代码如下:
require 'restclient'
p12 = OpenSSL::PKCS12.new(File.read('client.p12'), 'password')
client = RestClient::Resource.new('https://example.com/',
:ssl_client_key => p12.key,
:verify_ssl => OpenSSL::SSL::VERIFY_NONE)
client.get
Run Code Online (Sandbox Code Playgroud)
当我运行它时,我看到以下故障:
1.9.3-p374 :007 > client.get
RestClient::BadRequest: 400 Bad Request
from /home/duncan/.rvm/gems/ruby-1.9.3-p374/gems/rest-client-1.6.7/lib/restclient/abstract_response.rb:48:in `return!'
from /home/duncan/.rvm/gems/ruby-1.9.3-p374/gems/rest-client-1.6.7/lib/restclient/request.rb:230:in `process_result'
from /home/duncan/.rvm/gems/ruby-1.9.3-p374/gems/rest-client-1.6.7/lib/restclient/request.rb:178:in `block in transmit'
from /home/duncan/.rvm/rubies/ruby-1.9.3-p374/lib/ruby/1.9.1/net/http.rb:745:in `start'
from /home/duncan/.rvm/gems/ruby-1.9.3-p374/gems/rest-client-1.6.7/lib/restclient/request.rb:172:in `transmit'
from /home/duncan/.rvm/gems/ruby-1.9.3-p374/gems/rest-client-1.6.7/lib/restclient/request.rb:64:in `execute'
from /home/duncan/.rvm/gems/ruby-1.9.3-p374/gems/rest-client-1.6.7/lib/restclient/request.rb:33:in `execute'
from /home/duncan/.rvm/gems/ruby-1.9.3-p374/gems/rest-client-1.6.7/lib/restclient/resource.rb:51:in `get'
from (irb):7
from /home/duncan/.rvm/rubies/ruby-1.9.3-p374/bin/irb:13:in `<main>'
Run Code Online (Sandbox Code Playgroud)
我很确定这是一次验证失败,因为如果我没有安装客户端证书,我会在浏览器中遇到同样的错误.
我正在使用,OpenSSL::SSL::VERIFY_NONE因为服务器有一个自签名证书,我相信这是通过忽略它的正确值.
任何有关如何使这项工作的建议将不胜感激 - 即使指向一些详细的文档,或建议不同的宝石可以工作.我对Gem docs或Google没有太多运气:(
除了移动应用程序之外,我还需要在我的一个Web应用程序中集成Kii MbaaS服务.我使用Android SDK并能够连接它.但是对于使用Java解决方案的网站,他们没有任何SDK,并要求我使用REST进行操作.现在我计划使用Servlet中的ApacheHttpClient连接到REST服务.其文档中的REST格式如下所示.在ApacheHttpClient中,我知道我可以将头文件(-H)作为HttpGet.addHeader("content-type","application/json")传递.但是我不确定如何传递数据(-d).任何人都可以通过指向任何教程链接或任何有关如何将数据与标题一起传递到REST服务的示例代码来帮助我吗?
REST语法如下 -
curl -v -X POST \
-H "content-type:application/json" \
-H "x-kii-appid:{APP_ID}" \
-H "x-kii-appkey:{APP_KEY}" \
"https://api.kii.com/api/oauth2/token" \
-d '{"username":"user_123456", "password":"123ABC"}'
Run Code Online (Sandbox Code Playgroud)
提前致谢.
-------------------------编辑------------------------ --------------------------这里是我用连接到Apache HttpClient 4.3库编写的示例java代码但是我一直收到400错误. ..任何人都可以建议吗?
错误 -
线程"main"中的异常java.lang.RuntimeException:失败:在com.app.test.RestClientTest.main上的HTTP错误代码:400(RestClientTest.java:49)
package com.app.test;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.Consts;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
public class RestClientTest {
/**
* @param args
*/
public static void main(String[] args) {
CloseableHttpClient httpClient …Run Code Online (Sandbox Code Playgroud) 我正在使用Ruby gem rest-client(1.6.7)使用HTTP GET请求检索数据.但是,有时响应比我想要处理的大,所以我希望有一些方法可以在RestClient超过我设置的大小限制时停止读取.该文件说:
对于通用API未涵盖的情况,您可以使用提供较低级API的RestClient :: Request类.
但我不明白这对我有什么帮助.我没有看到任何看起来像处理传入数据流的钩子的东西,只有在读完整个东西后我才能执行的操作.我不想浪费时间和内存将大量响应读入缓冲区而只是丢弃它.
如何设置上的数据的读出的数量是有限RestClient的GET请求?或者是否有一个我可以使用的不同客户端可以轻松设置这样的限制?
我刚开始学习Ruby,经过一些基本的事情,我试图理解在ruby中对REST服务进行REST调用.我可以毫不费力地向foursquare API提出请求.另一方面,对Cisco CMX API的调用会出错.我的ruby版本是2.1.2.我在网上搜索了很多解决方案,但仍然遇到问题.这是我运行的shell命令.
工作一
$resclient
>> RestClient.get 'https://api.foursquare.com/v2/venues/search?ll=40.7,-74&oauth_token=0ZDO1JMJ0PW2QTCDK50OGZ21UENHZ0Y3KIDQZJLLURTQNRQ2&v=20150106'
Run Code Online (Sandbox Code Playgroud)
这给出了错误
$restclient
>> RestClient.get 'https://learning:learning@64.103.26.61/api/contextaware/v1/maps/.json'
Run Code Online (Sandbox Code Playgroud)
我的错误日志:
OpenSSL::SSL::SSLError: hostname "64.103.26.61" does not match the server certificate
from /Users/apple/.rbenv/versions/2.1.2/lib/ruby/2.1.0/openssl/ssl.rb:139:in `post_connection_check'
from /Users/apple/.rbenv/versions/2.1.2/lib/ruby/2.1.0/net/http.rb:922:in `connect'
from /Users/apple/.rbenv/versions/2.1.2/lib/ruby/2.1.0/net/http.rb:863:in `do_start'
from /Users/apple/.rbenv/versions/2.1.2/lib/ruby/2.1.0/net/http.rb:852:in `start'
from /Users/apple/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/rest-client-1.7.2/lib/restclient/request.rb:413:in `transmit'
from /Users/apple/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/rest-client-1.7.2/lib/restclient/request.rb:176:in `execute'
from /Users/apple/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/rest-client-1.7.2/lib/restclient/request.rb:41:in `execute'
from /Users/apple/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/rest-client-1.7.2/lib/restclient.rb:65:in `get'
from (irb):3
from /Users/apple/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/rest-client-1.7.2/bin/restclient:93:in `<top (required)>'
from /Users/apple/.rbenv/versions/2.1.2/bin/restclient:23:in `load'
from /Users/apple/.rbenv/versions/2.1.2/bin/restclient:23:in `<main>'
Run Code Online (Sandbox Code Playgroud)
你能给点建议吗?谢谢
我正在尝试使用rest API下载文件,它似乎可以正常工作,但是我实际上没有下载文件。我假设它是因为它将存储在内存中,而不是存储在我的文件系统中。
以下是负责的代码部分。在下面粘贴我的URL时,对其进行了稍微的编辑,并且我的authToken有效。
backup_url = "#{proto}://#{my_host}/applications/ws/migration/export?noaudit=#{include_audit}&includebackup=#{include_backup_zips}&authToken=#{my_token}"
resource = RestClient::Resource.new(
backup_url,
:timeout => nil,
:open_timeout => nil)
response = resource.get
if response.code == 200
puts "Backup Complete"
else
puts "Backup Failed"
abort("Response Code was not 200: Response Code #{response.code}")
end
Run Code Online (Sandbox Code Playgroud)
返回值:
# => 200 OK | application/zip 222094570 bytes
Backup Complete
Run Code Online (Sandbox Code Playgroud)
但是没有文件。
谢谢,
在Elasticsearch服务器中索引(存储)文档时,我们的应用程序encoutner超时异常。它不经常发生,但大约每天一次。这是详细信息。
pom.xml
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>6.0.0</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
初始化
@Bean
public RestHighLevelClient buildHighLevelClient() {
RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(httplist.toArray(new HttpHost[]{})));
return client;
}
Run Code Online (Sandbox Code Playgroud)
异常信息
java.lang.RuntimeException: error while performing request
at org.elasticsearch.client.RestClient$SyncResponseListener.get(RestClient.java:682)
at org.elasticsearch.client.RestClient.performRequest(RestClient.java:220)
at org.elasticsearch.client.RestClient.performRequest(RestClient.java:192)
at org.elasticsearch.client.RestHighLevelClient.performRequest(RestHighLevelClient.java:428)
at org.elasticsearch.client.RestHighLevelClient.performRequestAndParseEntity(RestHighLevelClient.java:414)
at org.elasticsearch.client.RestHighLevelClient.bulk(RestHighLevelClient.java:229)
......
Caused by: java.util.concurrent.TimeoutException
at org.apache.http.nio.pool.AbstractNIOConnPool.processPendingRequest(AbstractNIOConnPool.java:364)
at org.apache.http.nio.pool.AbstractNIOConnPool.processNextPendingRequest(AbstractNIOConnPool.java:344)
at org.apache.http.nio.pool.AbstractNIOConnPool.release(AbstractNIOConnPool.java:318)
at org.apache.http.impl.nio.conn.PoolingNHttpClientConnectionManager.releaseConnection(PoolingNHttpClientConnectionManager.java:303)
at org.apache.http.impl.nio.client.AbstractClientExchangeHandler.releaseConnection(AbstractClientExchangeHandler.java:239)
Run Code Online (Sandbox Code Playgroud) timeoutexception java.util.concurrent rest-client elasticsearch
rest-client ×10
java ×4
rest ×4
ruby ×4
jersey ×1
kiicloud ×1
microprofile ×1
openssl ×1
restlet ×1
ssl ×1