我使用Apache HttpClient访问网页.我想点击链接,但链接是javaScript,我如何处理点击JavaScript链接并遵循网址重定向?
示例javascript和html代码:
<a href="javascript:send(32023, 'YGHN_JKM', '8LMK');"> link</a>
function send(content_id, fic, cgRate) {
var params = new Hash();
params.set('content_id', content_id);
params.set('tool', fic);
params.set('cgRate', cgRate);
new Ajax.Updater('return', '/mypkg/tools', {
method: 'post',
parameters: params,
evalScripts: true,
onInitialize: new Effect.Appear('loader', {duration: 0.0}),
onComplete: new Effect.Fade('loader', {duration: 1.2})
});
}
Run Code Online (Sandbox Code Playgroud) 我使用Apache Commons HttpClient和Restlet来调用一个宁静的Web服务.不幸的是,我的服务器(基于Ruby on Rails)不喜欢Transfer-Encoding: chunkedHttpClient默认使用的服务器.
有没有办法禁止从客户端使用分块编码的POST?
我正在使用Apache Commons HttpClient v3.1.我的所有请求都在请求行中具有正确的(默认)HTTP版本,即HTTP/1.1,除了1个请求.
在Post请求之后获取请求行为HTTP/0.9:
server : port/cas/v1/tickets/TGT-1-sUqenNbqUzvkGSWW25lcbaJc0OEcJ6wg5DOj3XDMSwoIBf6s7i-cas-1
Body: service=*
Run Code Online (Sandbox Code Playgroud)
我通过http客户端代码调试并看到请求行设置为HTTP/1.1但在服务器上我看到请求是HTTP/0.9.
我尝试使用明确设置HTTP版本,HttpMethodParams但这没有帮助.有谁知道什么可能是错的?
HttpClient client = new HttpClient();
HostConfiguration hc = client.getHostConfiguration();
hc.setHost(new URI(url, false));
PostMethod method = new PostMethod();
method.setURI(new URI(url, false));
method.getParams().setUriCharset("UTF-8");
method.getParams().setHttpElementCharset("UTF-8");
method.getParams().setContentCharset("UTF-8");
method.getParams().setVersion(HttpVersion.HTTP_1_1);
method.addParameter("service", URLEncoder.encode(service, "UTF-8"));
method.setPath(contextPath + "/tickets/" + tgt);
String respBody = null;
int statusCode = client.executeMethod(method);
respBody = method.getResponseBodyAsString();
Run Code Online (Sandbox Code Playgroud) 我正在研究如何抓取信息.关于http客户端如何与jsoup相关,我有点困惑.你需要http客户端使用jsoup,还是jsoup可以替换http客户端?如果你仍然需要http客户端,它执行的功能是什么,jsoup不能自己做?
我想知道,使用 HttpClient 和 HttpPOST 有没有办法将复杂的 JSON 对象作为请求的正文发布?我确实看到了在正文中发布一个简单的键/值对的示例(如下图来自此链接:Http Post With Body):
HttpClient client= new DefaultHttpClient();
HttpPost request = new HttpPost("www.example.com");
List<NameValuePair> pairs = new ArrayList<NameValuePair>();
pairs.add(new BasicNameValuePair("paramName", "paramValue"));
request.setEntity(new UrlEncodedFormEntity(pairs ));
HttpResponse resp = client.execute(request);
Run Code Online (Sandbox Code Playgroud)
但是,我需要发布如下内容:
{
"value":
{
"id": "12345",
"type": "weird",
}
}
Run Code Online (Sandbox Code Playgroud)
有没有办法让我做到这一点?
附加信息
执行以下操作:
HttpClient client= new DefaultHttpClient();
HttpPost request = new HttpPost("www.example.com");
String json = "{\"value\": {\"id\": \"12345\",\"type\": \"weird\"}}";
StringEntity entity = new StringEntity(json);
request.setEntity(entity);
request.setHeader("Content-type", "application/json");
HttpResponse resp = client.execute(request);
Run Code Online (Sandbox Code Playgroud)
结果在服务器上是空的……因此我得到了 400。
提前致谢!
java httpclient apache-commons-httpclient apache-httpclient-4.x
我正在尝试使用Apache Client 4.5.5连接到我的Web服务器之一。
我正在尝试使用SSLContextBuilder,但现在似乎已弃用它,我不想使用已弃用的东西。
有人可以建议最新的处理无效认证的方法吗?
下面是我的代码,它工作正常,但是不建议使用SSLContext,SSLContextBuilder,loadTrustmaterial。
SSLContextBuilder sscb = new SSLContextBuilder();
sscb.loadTrustMaterial(null, new org.apache.http.conn.ssl.TrustSelfSignedStrategy());
SSLContext sslContext = sscb.build();
SSLConnectionSocketFactory sslSocketFactory =
new SSLConnectionSocketFactory(sslContext, new org.apache.http.conn.ssl.DefaultHostnameVerifier());
CloseableHttpClient httpclient = HttpClients.custom()
.setDefaultCredentialsProvider(credsProvider)
.setSSLSocketFactory(sslSocketFactory)
.setConnectionManager(connectionMgr)
.build();
try {
HttpPost httppost = new HttpPost("myURL");
Run Code Online (Sandbox Code Playgroud) 如果希望凭据(cookie身份验证令牌)可通过调用传递,则需要添加{ withCredentials: true }httpclient调用。像这样:
import { HttpClient } from '@angular/common/http';
...
constructor(private httpclient: HttpClient) { }
this.httpclient.get(url, { withCredentials: true })
Run Code Online (Sandbox Code Playgroud)
我只想知道是否有一种方法可以{ withCredentials: true }对每个呼叫进行预设。我不想{ withCredentials: true }每次打电话时都添加。
这是一个相关的问题,但是我不确定这是否适用于HttpClient?