我有一个基于.NET 4构建的控制台应用程序,它使用HttpClient库(通过NuGet获取)通过Internet从公共API检索数据.控制台应用程序位于代理后面.它所在的Windows机器在Internet Explorer中具有正确的代理设置.当控制台应用程序尝试访问外部世界时,它将获得407状态 - "需要代理身份验证".
我还没有写任何特定的代码来处理代理.我假设从407错误,应用程序指向代理没有问题,但代理不验证请求.
我的问题是,是否可以设置HttpClient使用登录用户的凭据来验证代理?如果是这样,怎么样?
var client = new HttpClient(webRequestHandler);
client.PostAsync(RequestUri, MyContent);
Run Code Online (Sandbox Code Playgroud) 这让我很疯狂,我在任何地方设置ContentType标头,似乎无法让它停止发送text/plain.
在Fiddler中查看数据时,请求始终请求:
POST http:/domain.com HTTP/1.1
内容类型:text/plain; 字符集= utf-8的
using (var httpClient = new HttpClient())
{
var request = new HttpRequestMessage(HttpMethod.Post, "http://domain.com");
request.Content = new StringContent(Serialize(obj), Encoding.UTF8, "text/xml");
request.Content.Headers.Clear();
request.Content.Headers.ContentType = new MediaTypeHeaderValue("text/xml");
request.Headers.Clear();
request.Headers.Add("Content-Type","text/xml");
var response = await httpClient.SendAsync(request);
return await response.Content.ReadAsStringAsync();
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用API URL.我用以下代码调用API.
import {HttpClient, HttpClientModule, HttpParams} from "@angular/common/http";
@Injectable()
export class PropertyPrefService {
constructor(private http: HttpClient,
private configurationService:Configuration) {}
public searchProjects(propFilter:string):any{
let temp:any;
const options = propFilter ?
{
params: new HttpParams().set('query', propFilter)
} : {};
return this.http.get<any>(this.configurationService.propertySystemApiUrl, options)
.subscribe((results:any) => {
console.log(results);
});
}
Run Code Online (Sandbox Code Playgroud)
在Angular代码中,我没有得到任何响应,并且收到错误:
(未知网址)的Http失败响应:0未知错误".
但是,当我在Chrome上打开开发人员工具时发出请求时,我看到响应是从服务器收到的.
URL是"https:// ..."URL而不是"http:// ...".
我从一个网页抓取的JSON数组有奇怪的字符编码问题.服务器正在发送回此标头:
内容类型文本/ javascript; 字符集= UTF-8
此外,我可以查看Firefox或任何浏览器中的JSON输出,并正确显示Unicode字符.响应有时会包含来自其他语言的单词,带有重音符号等.但是当我把它拉下来并把它放到Java中的字符串时,我得到那些奇怪的问号.这是我的代码:
HttpParams params = new BasicHttpParams();
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
HttpProtocolParams.setContentCharset(params, "utf-8");
params.setBooleanParameter("http.protocol.expect-continue", false);
HttpClient httpclient = new DefaultHttpClient(params);
HttpGet httpget = new HttpGet("http://www.example.com/json_array.php");
HttpResponse response;
try {
response = httpclient.execute(httpget);
if(response.getStatusLine().getStatusCode() == 200){
// Connection was established. Get the content.
HttpEntity entity = response.getEntity();
// If the response does not enclose an entity, there is no need
// to worry about connection release
if (entity != null) {
// A Simple JSON Response Read
InputStream instream …Run Code Online (Sandbox Code Playgroud) 我正在使用org.apache.http,我有这个代码:
DefaultHttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet(url);
HttpResponse resp = client.execute(get);
HttpEntity entity = resp.getEntity();
InputStream input = entity.getContent();
...
//Read the bytes from input stream
Run Code Online (Sandbox Code Playgroud)
这是我用来通过Http下载文件的代码,我想取消连接(可能是用户选择的)什么是关闭连接的优雅方式.我找到了两种方法,两种都取消了下载.
我尝试捕获,所以没有错误,但没有抛出异常,有没有办法取消或中止连接?
什么是正确的方法呢?
在Android 2.1/2.2上我使用DefaultHttpClientAndroid SDK中找到的.
Apache在他们的文档中说有两个超时:
CoreConnectionPNames.SO_TIMEOUT ='http.socket.timeout':定义套接字超时(SO_TIMEOUT),以毫秒为单位,即等待数据的超时,换句话说,两个连续数据包之间的最大周期不活动.超时值为零被解释为无限超时.此参数需要java.lang.Integer类型的值.如果未设置此参数,则读取操作不会超时(无限超时).
CoreConnectionPNames.CONNECTION_TIMEOUT ='http.connection.timeout':确定建立连接之前的超时(以毫秒为单位).超时值为零被解释为无限超时.此参数需要java.lang.Integer类型的值.如果未设置此参数,则连接操作不会超时(无限超时).
我尝试在Android源中搜索这两个超时的默认值,但无法找到.有谁知道这些超时的默认值是什么?我想获得一个链接到值设置的源或关于此的官方文档(而不仅仅是听取意见).
使用密钥库和信任库的KeyStore对象有什么区别; 而不是使用KeyManager和TrustManager?
让我解释一下我的要求.我正在使用RESTEasy,需要通过HTTPS使用SSL证书进行REST调用.我需要增加RESTEasy如何创建ClientRequest.这是我最初想到的:
public void afterPropertiesSet() throws Exception {
Assert.isTrue(StringUtils.isNotBlank(getKeystoreName()), "Key Store Name is Blank");
Assert.isTrue(StringUtils.isNotBlank(getKeystorePassword()), "Key Store Password is Blank.");
Assert.isTrue(StringUtils.isNotBlank(getKeystorePath()), "Key Store Path is Blank");
Assert.isTrue(StringUtils.isNotBlank(getTruststoreName()), "Trust Store Name is Blank");
Assert.isTrue(StringUtils.isNotBlank(getTruststorePassword()), "Trust Store Password is Blank.");
Assert.isTrue(StringUtils.isNotBlank(getTruststorePath()), "Trust Store Path is Blank");
// Set the keystore and truststore for mutual authentication
createKeystore();
createTruststore();
if (getHttpClient() == null) {
// Initialize HTTP Client
initializeHttpClient();
}
Assert.notNull(getHttpClient(), "HTTP Client is NULL after initialization");
}
public ClientRequest createClientRequest(String uri) throws …Run Code Online (Sandbox Code Playgroud) 当使用loopj AsyncHttpClient库时,我在发出请求时不断获取java.net.SocketTimeoutExceptions(见下文).
我可以设置一些超时值吗?
注意:我发布这个是希望为其他人提供一些帮助.我(愚蠢地)努力寻找解决方案一段时间.
堆栈跟踪:
java.net.SocketTimeoutException
at java.net.PlainSocketImpl.read(PlainSocketImpl.java:491)
at java.net.PlainSocketImpl.access$000(PlainSocketImpl.java:46)
at java.net.PlainSocketImpl$PlainSocketInputStream.read(PlainSocketImpl.java:240)
at org.apache.http.impl.io.AbstractSessionInputBuffer.fillBuffer(AbstractSessionInputBuffer.java:103)
at org.apache.http.impl.io.AbstractSessionInputBuffer.readLine(AbstractSessionInputBuffer.java:191)
at org.apache.http.impl.conn.DefaultResponseParser.parseHead(DefaultResponseParser.java:82)
at org.apache.http.impl.io.AbstractMessageParser.parse(AbstractMessageParser.java:174)
at org.apache.http.impl.AbstractHttpClientConnection.receiveResponseHeader(AbstractHttpClientConnection.java:180)
at org.apache.http.impl.conn.DefaultClientConnection.receiveResponseHeader(DefaultClientConnection.java:235)
at org.apache.http.impl.conn.AbstractClientConnAdapter.receiveResponseHeader(AbstractClientConnAdapter.java:259)
at org.apache.http.protocol.HttpRequestExecutor.doReceiveResponse(HttpRequestExecutor.java:279)
at org.apache.http.protocol.HttpRequestExecutor.execute(HttpRequestExecutor.java:121)
at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:428)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
at com.loopj.android.http.AsyncHttpRequest.makeRequest(AsyncHttpRequest.java:76)
at com.loopj.android.http.AsyncHttpRequest.makeRequestWithRetries(AsyncHttpRequest.java:95)
at com.loopj.android.http.AsyncHttpRequest.run(AsyncHttpRequest.java:57)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:390)
at java.util.concurrent.FutureTask.run(FutureTask.java:234)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
at java.lang.Thread.run(Thread.java:856)
Run Code Online (Sandbox Code Playgroud) 在Apache HttpClient 4.3版本上配置连接空闲超时的最短方法是什么?
我查看了文档,找不到任何东西.我的目标是将开放连接减少到最低服务器峰值.
例如,在Jetty Client 8.x中,您可以设置httpClient.setIdleTimeout:http://download.eclipse.org/jetty/stable-8/apidocs/org/eclipse/jetty/client/HttpClient.html#setIdleTimeout( long )
我正在尝试使用kerberos/HTTP主机进行身份验证.使用Apache HttpClient作为我的客户端 - 以及此源的略微修改版本. 我的Kerberos身份验证完全正常,我希望知道如何以编程方式设置登录凭据.目前,凭据是通过控制台手动输入的,但我希望在运行时由我选择.[实际上,我希望自动化并加载测试具有大量用户的服务器.].
编辑:这是相关部分的代码片段:
..
NegotiateSchemeFactory nsf = new NegotiateSchemeFactory();
httpclient.getAuthSchemes().register(AuthPolicy.SPNEGO, nsf);
Credentials use_jaas_creds = new Credentials() {
public String getPassword() {
return null;
}
public Principal getUserPrincipal() {
return null;
}
};
httpclient.getCredentialsProvider().setCredentials(
new AuthScope(null, -1, null),
use_jaas_creds);
HttpUriRequest request = new HttpGet("http://kerberoshost/");
HttpResponse response = httpclient.execute(request);
..
Run Code Online (Sandbox Code Playgroud)
该接口Credentials有两个方法- getPassword()和getUserPrincipal(),但是从一些调试我这样做,他们似乎并没有在所有被调用.
我在这里错过了什么?什么是静态设置凭据的更简洁方法?
之前已经提出过一个非常类似的问题,但是keytabs/login.conf hack过于繁琐,并且不适合使用大量用户凭据进行自动负载测试.感谢任何帮助.