我尝试使用以下代码调用WEB API并获取JSON输出。
我已经使用POSTMAN对此进行了测试,并且运行正常。标头中有两个值,POST请求的主体中有一些值。
但是当我尝试使用 Apache HTTP Client访问相同的WEB API时,我得到以下输出和错误:
Response Code : 200
Result:{"errorCode":3000,"errorMessage":"Invalid request parameters"}
代码:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicNameValuePair;
public class WhiteSourceAPI {
public static void main(String[] args) {
try {
String url = "https://example.com/api";
HttpClient client = HttpClientBuilder.create().build();
HttpPost post = new HttpPost(url);
//header
post.setHeader("Content-Type", "application/json");
post.setHeader("Accept-Charset", "UTF-8");
List<NameValuePair> urlParameters = new ArrayList<>();
urlParameters.add(new BasicNameValuePair("requestType", "xxxx"));
urlParameters.add(new BasicNameValuePair("projectToken", "xxxx-xxxx-xxxx-xxxx"));
post.setEntity(new UrlEncodedFormEntity(urlParameters));
HttpResponse response = client.execute(post);
System.out.println("Response Code : " + response.getStatusLine().getStatusCode());
BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
StringBuilder result = new StringBuilder();
String line = "";
while ((line = rd.readLine()) != null) {
result.append(line);
}
System.out.println("Result:" +result);
} catch (IOException ex) {
Logger.getLogger(WhiteSourceAPI.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
答案 0 :(得分:0)
正如@VitalyZ所提到的,我不应该使用UrlEncodedFormEntity作为正文,而是使用原始JSON。
toSort = "<div class='touchpoint_filterattributes' id='BODIVID_1571' style = 'background:#b7b7b7;'><div class='color_box_label' style = 'color:#000000;'>Customer Experience Team</div></div><br/><div class='touchpoint_filterattributes' id='BODIVID_1567' style = 'background:#b7b7b7;'><div class='color_box_label' style = 'color:#000000;'>1234</div></div><br/><div class='touchpoint_filterattributes' id='BODIVID_1569' style = 'background:#b7b7b7;'><div class='color_box_label' style = 'color:#000000;'>Claims</div></div><br/><div class='touchpoint_filterattributes' id='BODIVID_1266' style = 'background:#C9C9C9;'><div class='color_box_label' style = 'color:#000000;'>Claims</div></div><br/><div class='touchpoint_filterattributes' id='BODIVID_1570' style = 'background:#b7b7b7;'><div class='color_box_label' style = 'color:#000000;'>Corporate Communication</div></div><br/><div class='touchpoint_filterattributes' id='BODIVID_1260' style='background:#C9C9C9;'><div class='color_box_label' style = 'color:#000000;'>Claims</div></div><br/>";
a = toSort.split("<br/>");
obj = {}
for(key in a){
id = a[key].substr(53,4);
obj[id] = a[key];
}
newhtml = "";
for(key in obj){
newhtml += obj[key]+"<br/>";
}
console.log(newhtml);