我有基于休息的服务,我必须将XML传递给它,当我在RESTCLIENT ANS POSTMAN中运行时,它的运行正确,但是当我以代码运行它时,它会给我400糟糕请求代码在下面提供的任何类型的帮助都会有所帮助。谢谢
public static void callservice() throws URISyntaxException {
String USER_AGENT = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.1";
HttpClient httpClient = new DefaultHttpClient();
try {
HttpPost post = new HttpPost();
URI as = new URI("http://172.24.1.137:8280/finalApi/v1.0.0");
post.setURI(as);
post.setHeader("Authorization",
"Bearer 62fbb099-af9c-35a4-b8e5-82adf92ae9bd");
post.setHeader("SOAPAction",
"http://tempuri.org/IService/accountbalance");
post.setHeader("content-type", "text/xml; charset=utf-8");
post.setHeader("cache-control", "no-cache");
List<NameValuePair> urlParameters = new ArrayList<NameValuePair>();
StringBuffer xmlString = new StringBuffer();
xmlString
.append("<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/">");
xmlString.append("<soapenv:Header/>");
xmlString.append("<soapenv:Body>");
xmlString.append("<tem:accountbalance>");
xmlString.append("<tem:accountNo>051000207960141</tem:accountNo>");
xmlString.append("</tem:accountbalance>");
xmlString.append("</soapenv:Body>");
xmlString.append("</soapenv:Envelope>");
String stw = new String(
"<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/">rn <soapenv:Header/>rn <soapenv:Body>rn <tem:accountbalance>rn <!--Optional:-->rn <tem:accountno>051000207960141</tem:accountno>rn </tem:accountbalance>rn </soapenv:Body>rn</soapenv:Envelope>");
urlParameters.add(new BasicNameValuePair("xmldoc", xmlString
.toString()));
System.out.println(xmlString);
// urlParameters.add(new BasicNameValuePair("xml",
// xmlString.toString()));
post.setEntity(new UrlEncodedFormEntity(urlParameters));
// Execute HTTP request
HttpResponse httpResponse = httpClient.execute(post);
System.out.println("----------------------------------------");
System.out.println(httpResponse.getStatusLine());
System.out.println(httpResponse.getStatusLine().getStatusCode());
System.out.println(httpResponse.getParams());
System.out.println("----------------------------------------");
// Get hold of the response entity
HttpEntity entity = httpResponse.getEntity();
// If the response does not enclose an entity, there is no need
// to bother about connection release
byte[] buffer = new byte[1024];
if (entity != null) {
InputStream inputStream = entity.getContent();
try {
int bytesRead = 0;
BufferedInputStream bis = new BufferedInputStream(
inputStream);
while ((bytesRead = bis.read(buffer)) != -1) {
String chunk = new String(buffer, 0, bytesRead);
System.out.println(chunk);
}
} catch (IOException ioException) {
// In case of an IOException the connection will be released
// back to the connection manager automatically
ioException.printStackTrace();
} catch (RuntimeException runtimeException) {
// In case of an unexpected exception you may want to abort
// the HTTP request in order to shut down the underlying
// connection immediately.
post.abort();
runtimeException.printStackTrace();
} finally {
// Closing the input stream will trigger connection release
try {
inputStream.close();
} catch (Exception ignore) {
}
}
}
} catch (ClientProtocolException e) {
// thrown by httpClient.execute(httpGetRequest)
e.printStackTrace();
} catch (IOException e) {
// thrown by entity.getContent();
e.printStackTrace();
} finally {
// When HttpClient instance is no longer needed,
// shut down the connection manager to ensure
// immediate deallocation of all system resources
httpClient.getConnectionManager().shutdown();
}
}
您需要在API所在的目标服务器上允许跨域请求。看到这个: - 如何在服务器上启用跨域请求?