public class HttpPos {
public static void main(String[] args) throws ClientProtocolException, IOException {
try {
DefaultHttpClient httpclient = new DefaultHttpClient(); httpclient.getCredentialsProvider().setCredentials(
new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT),
new UsernamePasswordCredentials("admin@internal", "111111"));
HttpPost httppost = new HttpPost("http://192.168.8.66:8080/api/vms/f2fb2529-cddb-4fde-ab16-6cc5ee0eb9c5/start");
httppost.setHeader("Content-Type", "application/xml");
System.out.println("executing request " + httppost.getRequestLine());
HttpResponse response;
response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
httppost.setEntity(entity);
System.out.println(response.getStatusLine().getStatusCode());
System.out.println("----------------------------------------");
System.out.println(response.getStatusLine());
if (entity != null) {
System.out.println("Response content length: " + entity.getContentLength());
}
httpclient.getConnectionManager().shutdown();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void main(String[] args) throws ClientProtocolException, IOException {
try {
DefaultHttpClient httpclient = new DefaultHttpClient(); httpclient.getCredentialsProvider().setCredentials(
new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT),
new UsernamePasswordCredentials("admin@internal", "111111"));
HttpPost httppost = new HttpPost("http://192.168.8.66:8080/api/vms/f2fb2529-cddb-4fde-ab16-6cc5ee0eb9c5/start");
httppost.setHeader("Content-Type", "application/xml");
System.out.println("executing request " + httppost.getRequestLine());
HttpResponse response;
response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
httppost.setEntity(entity);
System.out.println(response.getStatusLine().getStatusCode());
System.out.println("----------------------------------------");
System.out.println(response.getStatusLine());
if (entity != null) {
System.out.println("Response content length: " + entity.getContentLength());
}
httpclient.getConnectionManager().shutdown();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
这是请求语法有问题。看看参数啥的是否写错了。