我正在尝试使用以下代码登录网站:
require 'rubygems'
require 'nokogiri'
require 'open-uri'
require 'csv'
require 'restclient'
HEADERS_HASH = {"User-Agent" => "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_4) AppleWebKit/534.57.5 (KHTML, like Gecko) Version/5.1.7 Safari/534.57.4"}
page = Nokogiri::HTML(open("http://example.com/login", HEADERS_HASH))
token = page.css("form.login_box div input")[0]['value']
login_resp = RestClient.post("https://example.com/session", {"authenticity_token" => token, "login" => 'username', "password" => 'password', "remember_me" => 1, 'commit' => 'Sign In'})
Run Code Online (Sandbox Code Playgroud)
但是我收到以下错误:
/usr/local/Cellar/ruby/1.9.3-p194/lib/ruby/gems/1.9.1/gems/rest-client-1.6.7/lib/restclient/abstract_response.rb:39:in `return!': 302 Found (RestClient::Found)
from /usr/local/Cellar/ruby/1.9.3-p194/lib/ruby/gems/1.9.1/gems/rest-client-1.6.7/lib/restclient/request.rb:230:in `process_result'
from /usr/local/Cellar/ruby/1.9.3-p194/lib/ruby/gems/1.9.1/gems/rest-client-1.6.7/lib/restclient/request.rb:178:in `block in transmit'
from /usr/local/Cellar/ruby/1.9.3-p194/lib/ruby/1.9.1/net/http.rb:745:in `start'
from /usr/local/Cellar/ruby/1.9.3-p194/lib/ruby/gems/1.9.1/gems/rest-client-1.6.7/lib/restclient/request.rb:172:in `transmit'
from /usr/local/Cellar/ruby/1.9.3-p194/lib/ruby/gems/1.9.1/gems/rest-client-1.6.7/lib/restclient/request.rb:64:in `execute'
from …Run Code Online (Sandbox Code Playgroud) 我使用的是最新版本的rest-client gem,在外部访问时我看到很多RestClient :: ServerBrokeConnection错误,我该如何处理呢?
以下呼叫失败
response = RestClient::Request.execute(method: :post, url: url, headers: headers, "Content-Type" => "application/x-www-form-urlencoded")
Run Code Online (Sandbox Code Playgroud) 这个使用 Ajax 的 POST 请求完美地工作:
var token = "my_token";
function sendTextMessage(sender, text) {
$.post('https://graph.facebook.com/v2.6/me/messages?',
{ recipient: {id: sender},
message: {text:text},
access_token: token
},
function(returnedData){
console.log(returnedData);
});
};
sendTextMessage("100688998246663", "Hello");
Run Code Online (Sandbox Code Playgroud)
我需要有相同的请求,但在 Ruby 中。我尝试使用 Net:HTTP,但它不起作用,我没有收到任何错误,所以我无法调试它:
token = "my_token"
url = "https://graph.facebook.com/v2.6/me/messages?"
sender = 100688998246663
text = "Hello"
request = {
recipient: {id: sender},
message: {text: text},
access_token: token
}.to_json
uri = URI.parse(url)
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
request = Net::HTTP::Post.new(uri.request_uri)
response = http.request(request)
response.body
Run Code Online (Sandbox Code Playgroud)
我应该如何继续获取错误或我哪里出错了?
我正在使用RestClient和Ruby v.2.2.1尝试使用内部测试API服务器.
这基本上是代码:
url = "https://10.10.0.10/thing/i/want/to/get"
header = {
:content_type => "application/json",
:"x-auth-token" => "testingtoken"
}
response = RestClient.get url, header
Run Code Online (Sandbox Code Playgroud)
这是我收到的失败消息:
SSL_connect returned=1 errno=0 state=SSLv3 read server certificate B: certificate verify failed (RestClient::SSLCertificateNotVerified)
Run Code Online (Sandbox Code Playgroud)
如果我正确阅读,看起来Ruby无法接受SSL安全证书.此调用适用于Chrome应用Postman,但为了使其正常工作,我必须点击Chrome本身的网址并接受连接不安全(但仍然继续),然后它将在邮递员中工作.
有没有办法忽略证书失败并继续在Ruby中继续?
当我执行 POST 请求时,我收到此错误。错误:连接 ECONNREFUSED 127.0.0.1:3000。相反,当我使用邮递员时,它可以工作。
这是网址: http://localhost:3000/users/sign_in
这是我的配置:
JSON 选项卡:
{"user": {"email": "user@nivelr.com", "password": "clave12345"}}
Run Code Online (Sandbox Code Playgroud)
标题:
我想<MYURL>/myaddres?path=<MYPATH>&content=<MYCONTENT>在 IntelliJ 中使用基于编辑器的 REST 客户端发送到服务,但从<MYCONTENT>文件中获取内容。
是否可以?
使用 VSCode Rest Client - 如何在变量之间传递变量的值?
@varA = “123”
@varB = “234”
@varFinal = varA
Run Code Online (Sandbox Code Playgroud)
目前它不传递值,它将 varFinal 设置为静态字符串 'varA' - 我希望它是 '123'
我在使用带有相互身份验证的 spring Resttemplate 发出发布请求时收到上述错误。
@Bean
public RestTemplate restTemplate() throws UnrecoverableKeyException,
NoSuchAlgorithmException, KeyStoreException, IOException, KeyManagementException, CertificateException {
KeyStore clientStore = KeyStore.getInstance("PKCS12");
clientStore.load(new FileInputStream(pfxFile), pfxPass.toCharArray());
SSLContextBuilder sslContextBuilder = new SSLContextBuilder();
sslContextBuilder.useProtocol("TLS");
sslContextBuilder.loadKeyMaterial(clientStore, pfxPass.toCharArray());
sslContextBuilder.loadTrustMaterial(new TrustSelfSignedStrategy());
SSLConnectionSocketFactory sslConnectionSocketFactory = new SSLConnectionSocketFactory(sslContextBuilder.build());
CloseableHttpClient httpClient = HttpClients.custom()
.setSSLSocketFactory(sslConnectionSocketFactory)
.build();
HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(httpClient);
requestFactory.setConnectTimeout(Integer.parseInt(timeOut)); // 10 seconds
requestFactory.setReadTimeout(Integer.parseInt(timeOut)); // 10 seconds
RestTemplate restTemplate = new RestTemplate(requestFactory);
restTemplate.setInterceptors( Collections.singletonList(new RequestResponseLoggingInterceptor()));
return restTemplate;
}
Run Code Online (Sandbox Code Playgroud)
使用resttemplate的代码如下
public ResponseEntity<OauthResponse> getOauthToken(String clientScope,
String BasicAuthUser,String BasicAuthPass){
String accessToken = Base64.getEncoder().encodeToString((BasicAuthUser+":"+BasicAuthPass).getBytes()); …Run Code Online (Sandbox Code Playgroud) 我有一个RestService接口,有许多休息调用,我在整个应用程序中使用它.
我正在为处理connection和设置超时read-timeouts
ClientHttpRequestFactory httpFactory = myRestService.getRestTemplate().getRequestFactory();
if(httpFactory!=null)
{
if(httpFactory instanceof SimpleClientHttpRequestFactory)
{
((SimpleClientHttpRequestFactory)httpFactory).setConnectTimeout(10*1000);
((SimpleClientHttpRequestFactory)httpFactory).setReadTimeout(30*1000);
}
else if(httpFactory instanceof HttpComponentsClientHttpRequestFactory)
{
((HttpComponentsClientHttpRequestFactory)httpFactory).setConnectTimeout(10*1000);
((HttpComponentsClientHttpRequestFactory)httpFactory).setReadTimeout(30*1000);
}
}
Run Code Online (Sandbox Code Playgroud)
但我坚持处理超时情况.我想过使用这种方法但是当休息调用失败时它不会进入这个循环.
myRestService.getRestTemplate().setErrorHandler(new ResponseErrorHandler()
{
@Override
public boolean hasError(ClientHttpResponse paramClientHttpResponse) throws IOException
{
Log.e(TAG, paramClientHttpResponse==null?"Null response" : ("Has Error : " + paramClientHttpResponse.getStatusText()+" , status code : "+paramClientHttpResponse.getStatusCode()));
return false;
}
@Override
public void handleError(ClientHttpResponse paramClientHttpResponse) throws IOException
{
Log.e(TAG, paramClientHttpResponse==null?"Null response":("Handle Error : " + paramClientHttpResponse.getStatusText()+" , status code : "+paramClientHttpResponse.getStatusCode()));
}
}); …Run Code Online (Sandbox Code Playgroud) 我正在使用Mozilla fire fox附加组件RESTClient来测试我的网络服务.到目前为止,我通过在请求主体中POST设置标题Content-Type:application/x-www-form-urlencoded和数据来使用方法,&例如id=1&title=abc
但现在我想POST的JSON对象使用相同的服务.示例对象如下:
[
{
"id": 4
"type":"alpha",
"title":"Title1"
}
]
Run Code Online (Sandbox Code Playgroud)
要么
[
{
"id": 4
"type":"alpha",
"title":"Alpha"
},
{
"id": 5
"type":"beta",
"title":"Beta"
},
{
"id": 6
"type":"gama",
"title":"Gama"
}
]
Run Code Online (Sandbox Code Playgroud)
要么
[
{
"id": 5,
"type":"beta",
"title":"Sample beta",
"children":[{
"id": 6,
"type":"betachild",
"title":"Beta child 1"
},
{
"id": 7,
"type":"betachild",
"title":"Beta child 2"
},
{
"id": 8,
"type":"betachild",
"title":"Beta child 3"
} …Run Code Online (Sandbox Code Playgroud)