我正在尝试发布到需要使用以下代码设置Content-Length标头的Web服务:
// EDIT: added apache connector code
ClientConfig clientConfig = new ClientConfig();
ApacheConnector apache = new ApacheConnector(clientConfig);
// setup client to log requests and responses and their entities
client.register(new LoggingFilter(Logger.getLogger("com.example.app"), true));
Part part = new Part("123");
WebTarget target = client.target("https://api.thing.com/v1.0/thing/{thingId}");
Response jsonResponse = target.resolveTemplate("thingId", "abcdefg")
.request(MediaType.APPLICATION_JSON)
.header(HttpHeaders.AUTHORIZATION, "anauthcodehere")
.post(Entity.json(part));
Run Code Online (Sandbox Code Playgroud)
从发布说明https://java.net/jira/browse/JERSEY-1617和Jersey 2.0文档https://jersey.java.net/documentation/latest/message-body-workers.html,它暗示Content-长度自动设定.但是,我从服务器返回411响应代码,表明请求中不存在Content-Length.
有谁知道获取Content-Length标头集的最佳方法?
我已通过设置记录器验证了请求中未生成Content-Length标头.
谢谢.
如何使用ThreadSafeClientConnectionManaget为阻塞读/写设置SSL套接字超时?我发现在读取或写入SSL套接字时丢失网络连接会导致Android OS 2.2和2.3设备超时15分钟.
我在HttpClient上设置了以下超时:
mParams = new BasicHttpParams();
HttpProtocolParams.setVersion(mParams, HttpVersion.HTTP_1_1);
HttpProtocolParams.setContentCharset(mParams, "UTF-8");
HttpProtocolParams.setUserAgent(mParams, USER_AGENT);
HttpConnectionParams.setConnectionTimeout(mParams, TIME_OUT);
HttpConnectionParams.setSoTimeout(mParams, TIME_OUT);
ConnManagerParams.setTimeout(mParams, TIME_OUT);
final SchemeRegistry registry = new SchemeRegistry();
registry.register(new Scheme(HTTP_SCHEME, PlainSocketFactory.getSocketFactory(), 80));
registry.register(new Scheme(HTTPS_SCHEME, SSLSocketFactory.getSocketFactory(), 443));
final ThreadSafeClientConnManager manager = new ThreadSafeClientConnManager(mParams, registry);
mClient = new DefaultHttpClient(manager, mParams);
Run Code Online (Sandbox Code Playgroud)
然后,我使用客户端执行带有文件实体的http put请求.如果我在上传中途打开飞行模式,等待15-30秒然后关闭飞行模式,插座将被卡在读或写中,并且不会超时15分钟.