我正在尝试使用以下代码在头文件中使用用户ID和密码调用来自java swing桌面应用程序(在泽西网络应用程序上实现)的登录api。
String authString = username + ":" + password;
String authStringEnc = new BASE64Encoder().encode(authString.getBytes());
System.out.println("Base64 encoded auth string: " + authStringEnc);
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpGet getRequest = new HttpGet(FVConstants.loginAPI);
getRequest.addHeader("Authorization", "Basic " + authStringEnc);
HttpResponse response = null;
try {
response = httpClient.execute(getRequest);
if (response.getStatusLine().getStatusCode() != 200) {
throw new RuntimeException("Failed : HTTP error code : "
+ response.getStatusLine().getStatusCode());
}
BufferedReader br = new BufferedReader(new InputStreamReader((response.getEntity().getContent())));
String output;
System.out.println("Output from Server .... \n");
while ((output = br.readLine()) != null) {
System.out.println(output);
}
httpClient.getConnectionManager().shutdown();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
它在response = httpClient.execute(getRequest);
Exception in thread "AWT-EventQueue-0" java.lang.NoSuchFieldError: DEF_CONTENT_CHARSET
at org.apache.http.impl.client.DefaultHttpClient.setDefaultHttpParams(DefaultHttpClient.java:175)
at org.apache.http.impl.client.DefaultHttpClient.createHttpParams(DefaultHttpClient.java:158)
at org.apache.http.impl.client.AbstractHttpClient.getParams(AbstractHttpClient.java:448)
at org.apache.http.impl.client.AbstractHttpClient.createClientConnectionManager(AbstractHttpClient.java:309)
at org.apache.http.impl.client.AbstractHttpClient.getConnectionManager(AbstractHttpClient.java:466)
at org.apache.http.impl.client.AbstractHttpClient.createHttpContext(AbstractHttpClient.java:286)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:851)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:805)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:784)
at login.Login.callLoginApi(Login.java:67)
at login.Login.actionPerformed(Login.java:101)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6533)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
我尝试从构建路径中删除同一个库的多个版本,并从HttpClient DEF_CONTENT_CHARS中提到的/.m2/repository/org/apache/中删除,并再次添加,仍然给我同样的错误。 非常感谢任何帮助,谢谢。
答案 0 :(得分:0)
使用以下代码解决。请确保您有以下导入。
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.HttpResponse;
使用这些导入添加库,您最好使用下面的代码。
private void callLoginApi(String username, String password)
{
String authString = username + ":" + password;
String authStringEnc = new
BASE64Encoder().encode(authString.getBytes());
System.out.println("Base64 encoded auth string: " + authStringEnc);
HttpClient client = HttpClientBuilder.create().build();
HttpGet request = new HttpGet(FVConstants.loginAPI);
request.addHeader("Authorization", "Basic " + authStringEnc);
try {
HttpResponse response = client.execute(request);
System.out.println("response is : "+response.getStatusLine());
} catch (Exception e) {
e.printStackTrace();
}
}