这是我的代码:
公共类 JavaSoapUi {
public static void main(String[] args) throws Exception {
String requestUrl = "myurl";
URLConnection connection =new URL(requestUrl).openConnection();
System.out.println( "orignal url: " + connection.getURL() );
connection.connect();
System.out.println( "connected url: " + connection.getURL() );
HttpPost httpPost = new HttpPost(requestUrl);
String username = "user";
String password = "pass";
String encoded = DatatypeConverter.printBase64Binary((username + ":" + password).getBytes("UTF-8"));
httpPost.addHeader("AUTHORIZATION", "Basic " + encoded);
HttpClient httpClient = HttpClientBuilder.create().build();
HttpResponse response = httpClient.execute(httpPost);
System.out.println("Response" + response.getStatusLine().toString());
HttpEntity entity = response.getEntity();
String responseString = EntityUtils.toString(entity, "UTF-8");
System.out.println("ResponseString" + responseString);
EntityUtils.consume(entity);
}
我得到输出错误:HTTP/1.1 301 Moved Permanently
如果我在 SOAP UI 中使用用户名和密码,通过使用基本身份验证,我可以获得输出。但是当我在 Java 代码中使用相同的用户名和密码时,我得到了错误。
任何人帮助我。