我收到错误“org.apache.http.ProtocolException: Not a valid protocol version: <html>
”当我尝试在example
HttpParams params = new SyncBasicHttpParams();
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
HttpProtocolParams.setContentCharset(params, "UTF-8");
HttpProtocolParams.setUserAgent(params, "Apache-HttpClient/4.0.1 (java 1.5)");
HttpProtocolParams.setUseExpectContinue(params, true);
HttpProcessor httpproc = new ImmutableHttpProcessor(new HttpRequestInterceptor[] {
// Required protocol interceptors
new RequestContent(),
new RequestTargetHost(),
// Recommended protocol interceptors
new RequestConnControl(),
new RequestUserAgent(),
new RequestExpectContinue()});
HttpRequestExecutor httpexecutor = new HttpRequestExecutor();
HttpContext context = new BasicHttpContext(null);
// Host to post
HttpHost host = new HttpHost(hostUrl, hostPort);
DefaultHttpClientConnection conn = new DefaultHttpClientConnection();
context.setAttribute(ExecutionContext.HTTP_CONNECTION, conn);
context.setAttribute(ExecutionContext.HTTP_TARGET_HOST, host);
try {
SocketAddress sockaddr = new InetSocketAddress(host.getHostName(), host.getPort());
Socket socket = new Socket();
socket.connect(sockaddr);
conn.bind(socket, params);
BasicHttpEntityEnclosingRequest request = new BasicHttpEntityEnclosingRequest("POST","");
HttpEntity requestBody = new StringEntity(xmlToPost, "UTF-8");
request.setEntity(requestBody);
request.setParams(params);
httpexecutor.preProcess(request, httpproc, context);
// FAILS HERE WITH THE MESSAGE "org.apache.http.ProtocolException: Not a valid protocol version: <html>"
HttpResponse response = httpexecutor.execute(request, conn, context);
response.setParams(params);
httpexecutor.postProcess(response, httpproc, context);
} catch (Exception e) {
}
我确定它不是服务器,因为当我在firefox中使用“Poster”插件时它可以正常工作。我正在尝试发布xml,我不确定是否或如何将其添加到此代码中。