如何使用HttpClient 4.1.1避免循环重定向.因为我得到这样的错误: -
executing requestGET http://home.somehost.com/Mynet/pages/cHome.xhtml HTTP/1.1
org.apache.http.client.ClientProtocolException
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:822)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:754)
at edu.uci.ics.crawler4j.url.WebURL.setURL(WebURL.java:122)
at edu.uci.ics.crawler4j.crawler.CrawlController.addSeed(CrawlController.java:207)
at edu.uci.ics.crawler4j.example.advanced.Controller.main(Controller.java:31)
Caused by: org.apache.http.client.CircularRedirectException: Circular redirect to 'http://home.somehost.com/Mynet/pages/Home.xhtml'
at org.apache.http.impl.client.DefaultRedirectStrategy.getLocationURI(DefaultRedirectStrategy.java:168)
at org.apache.http.impl.client.DefaultRedirectStrategy.getRedirect(DefaultRedirectStrategy.java:193)
at org.apache.http.impl.client.DefaultRequestDirector.handleResponse(DefaultRequestDirector.java:1021)
at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:482)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:820)
Run Code Online (Sandbox Code Playgroud)
这是我的代码......
DefaultHttpClient client = null;
try
{
// Set url
//URI uri = new URI(url.toString());
client = new DefaultHttpClient();
client.getCredentialsProvider().setCredentials(
new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, AuthScope.ANY_REALM),
new UsernamePasswordCredentials("test", "test"));
URL url1 = new URL (url);
HttpURLConnection connection = (HttpURLConnection) url1.openConnection();
connection.setFollowRedirects(false);
HttpGet request = new HttpGet(url);
final …Run Code Online (Sandbox Code Playgroud) 一切都在今天工作,直到它停止...以下是最小的源代码(我正在使用VS 2012 Update 1 .Net 4.5).当我运行它时,app会在调用client.PostAsync()时退出,因此它永远不会到达Console.ReadLine().在调试器中也一样,没有例外,没有,退出代码0.
我尝试重新启动机器,重新启动VS2012 - 没有任何作用.
同样,今天一切都在运行,不确定发生了什么变化(没有安装任何软件等,所有其他网络应用仍然有效).
有任何想法吗?我想我正在失去理智.
class Program
{
static void Main(string[] args)
{
Run();
}
private async static void Run()
{
using (var client = new System.Net.Http.HttpClient())
{
var headers = new List<KeyValuePair<string, string>>
{
new KeyValuePair<string, string>("submit.x", "48"),
new KeyValuePair<string, string>("submit.y", "15"),
new KeyValuePair<string, string>("submit", "login")
};
var content = new FormUrlEncodedContent(headers);
HttpResponseMessage response = await client.PostAsync("http://www.google.com/", content);
Console.ReadLine();
}
}
}
Run Code Online (Sandbox Code Playgroud) 我正在使用RestAssured 2.8.0并且我正在尝试设置自己的超时(对于网关超时),所以如果我在X毫秒后没有得到响应我想要中止.
我试过了:
public static ValidatableResponse postWithConnectionConfig(String url, String body, RequestSpecification requestSpecification, ResponseSpecification responseSpecification) {
ConnectionConfig.CloseIdleConnectionConfig closeIdleConnectionConfig = new ConnectionConfig.CloseIdleConnectionConfig(1L, TimeUnit.MILLISECONDS);
ConnectionConfig connectionConfig = new ConnectionConfig(closeIdleConnectionConfig);
RestAssuredConfig restAssuredConfig = new RestAssuredConfig().connectionConfig(connectionConfig);
return given().specification(requestSpecification)
.body(body)
.config(restAssuredConfig)
.post(url)
.then()
.specification(responseSpecification);
}
Run Code Online (Sandbox Code Playgroud)
要么
ConnectionConfig connectionConfig = new ConnectionConfig()
.closeIdleConnectionsAfterEachResponseAfter(10L, TimeUnit.MILLISECONDS);
RestAssuredConfig restAssuredConfig = new RestAssuredConfig().connectionConfig(connectionConfig);
Run Code Online (Sandbox Code Playgroud)
我也尝试添加
.queryParam("SO_TIMEOUT", 10)
Run Code Online (Sandbox Code Playgroud)
要么
.queryParam("CONNECTION_MANAGER_TIMEOUT", 10)
Run Code Online (Sandbox Code Playgroud)
似乎没什么用.它不会中止我的查询
我有一个小型 NodeExpress 服务器,当我尝试向服务器发出请求时,它返回 403 错误 我已经安装Cors并使用了它,并尝试使用 chrome 浏览器和 Postman 作为 2 个客户端,但得到了相同的拒绝错误
var express = require('express')
var cors = require('cors')
var app = express()
app.use(cors())
app.get('/', function (req, res, next) {
res.json({msg: 'This is CORS-enabled for all origins!'})
})
app.listen(80, function () {
console.log('CORS-enabled web server listening on port 80')
})
Run Code Online (Sandbox Code Playgroud) 如何使用与此代码等效的node.js发出Http请求:
curl -X PUT http://localhost:3000/users/1
Run Code Online (Sandbox Code Playgroud) 我需要向Web服务器发送一个POST请求,其中包含一个gzip压缩请求参数.我正在使用Apache HttpClient,我已经读过它支持开箱即用的Gzip,但我找不到任何如何做我需要的例子.如果有人能发布一些这方面的例子,我会很感激.
我有一段代码连接到Socket服务器,它工作正常.
Socket socket = new Socket();
socket.connect(new InetSocketAddress(address, port));
Run Code Online (Sandbox Code Playgroud)
现在我想通过HTTP代理连接,我该怎么办?
我试过这个并且失败了
SocketAddress proxyAddr = new InetSocketAddress(proxyHost, proxyPort);
Proxy proxy = new Proxy(Proxy.Type.SOCKS, addr);
Socket socket = new Socket(proxy);
socket.connect(new InetSocketAddress(address, port));
Run Code Online (Sandbox Code Playgroud)
这篇文章建议我应该使用Jakarta Commons HttpClient,但如何使用它通过HTTP代理连接Socket服务器?
更新:我使用SOCKS代理,如果我使用HTTP代理它不起作用:
SocketAddress proxyAddr = new InetSocketAddress(proxyHost, proxyPort);
Proxy proxy = new Proxy(Proxy.Type.HTTP, addr);
Socket socket = new Socket(proxy);
socket.connect(new InetSocketAddress(address, port));
Run Code Online (Sandbox Code Playgroud)
它会抛出IllegalArgumentException
java.lang.IllegalArgumentException: Proxy is null or invalid type
at java.net.Socket.<init>(Socket.java:88)
Run Code Online (Sandbox Code Playgroud) 我想向谷歌购物api发送一个HTTPS获取请求,但是没有什么对我有用,例如这里是我正在尝试的内容:
try {
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet();
request.setURI(new URI("https://www.googleapis.com/shopping/search/v1/public/products/?key={my_key}&country=&q=t-shirts&alt=json&rankByrelevancy="));
HttpResponse response = client.execute(request);
} catch (URISyntaxException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return response;
}
Run Code Online (Sandbox Code Playgroud)
如果有人对如何改进或更换它有任何建议,请提前通知我.
我想使用python从一些网站捕获信息.我希望http客户端满足这些条件:
我知道requests,tornado或者gevent-httpclient可以完成我的任务,但我不知道这是最好的?或者还有其他选择吗?
或者如果在C/中写入了其他一些选择c++.
谢谢!
我正在编写一个Angular SPA应用程序,该应用程序使用HttpClient从后端获取值。
告诉它不缓存的简单方法是什么?我第一次要求它获取值,然后它拒绝进行后续查询。
谢谢,格里
httpclient ×10
java ×3
http ×2
node.js ×2
.net-4.5 ×1
android ×1
angular ×1
async-await ×1
c# ×1
caching ×1
cors ×1
express ×1
get ×1
gzip ×1
https ×1
java-8 ×1
javascript ×1
python ×1
rest-assured ×1
sockets ×1