我正在尝试为Web API创建一个简单的发布请求并解析收到的响应。经过大量的搜索,我能够得到以下代码:
public class access {
public static void main(String[] args) {
HttpClient httpclient = HttpClients.createDefault();
HttpPost httppost = new HttpPost("https://xxxxx/RSAM_API/api/Logon");
httppost.setHeader("Accept", "application/json");
httppost.setHeader("Content-type", "application/json");
// Request parameters and other properties.
List<NameValuePair> urlParameters = new ArrayList<NameValuePair>(2);
urlParameters.add(new BasicNameValuePair("UserId", "xxxxxx"));
urlParameters.add(new BasicNameValuePair("Password", "xxxxxxx"));
try {
httppost.setEntity(new UrlEncodedFormEntity(urlParameters));
//Execute and get the response.
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
String line;
while(null !=(line=rd.readLine())){
System.out.println(line);
}
}
catch (Exception e){
e.printStackTrace();
}
}
}
在上面的代码中,我正在对URL登录端点发出post请求。解析后我看到的响应是:
An error occurred, please try again or contact the administrator with this error id, 26225
我可以通过浏览器手动登录URL。不知道我在代码中出错了。
首先我的方法是否正确?我可以确定我的代码是对的吗?如果是,为什么响应是错误的呢?
任何帮助将不胜感激。谢谢。