古いプログラマーで、Java は初めてです。私は、ウェブ上の多くの場所で似ている非常に一般的なサンプルコードであると思うものを実行しようとしていますがHttpClient httpClient = HttpClientBuilder.create().build()
、例外がスローされ、その理由がわかりません。私はHttpClient 4.3を使用しています。
import java.io.IOException;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
public class ATest{
public static void main(String[] args) throws Exception {
String strURL = "http://192.9.10.11/cgi-bin/echo";
String message = "hello world";
// next line throwsClassNotFoundException, why?
HttpClient httpClient = HttpClientBuilder.create().build();
HttpPost httpPost = new HttpPost(strURL);
httpPost.setEntity(new StringEntity(message));
HttpResponse response = httpClient.execute(httpPost);
try {
System.out.println(response.getStatusLine());
HttpEntity entity = response.getEntity();
// do something useful with the response body
// and ensure it is fully consumed
EntityUtils.consume(entity);
} finally {
response.close();
}
}
}