Web services 如何使用httpclient发送XML请求并接收文件?(爪哇)
我需要向一个Web服务发送一个XML请求,该Web服务不是使用SOAP协议开发的。webservice只适用于纯XML请求/应答,因此没有WSDL。Web服务将使用我必须下载的gzip文件进行应答。有人能帮我吗?我从下面的代码开始。谢谢Web services 如何使用httpclient发送XML请求并接收文件?(爪哇),web-services,xmlhttprequest,httpclient,Web Services,Xmlhttprequest,Httpclient,我需要向一个Web服务发送一个XML请求,该Web服务不是使用SOAP协议开发的。webservice只适用于纯XML请求/应答,因此没有WSDL。Web服务将使用我必须下载的gzip文件进行应答。有人能帮我吗?我从下面的代码开始。谢谢 import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
public class Teste {
public static void main(String[] args)
{
boolean success = XMLDataPost();
System.out.println(success);
}
private static boolean XMLDataPost(){
boolean success = false;
HttpClient httpclient = new DefaultHttpClient();
try {
HttpPost httpPost = new HttpPost("http://webservice.blablabla.com.br");
StringEntity reqEntity = new StringEntity("<RequestVeiculo><login>02566288000191</login><senha>159828</senha></RequestVeiculo>");
reqEntity.setContentType("text/xml");
reqEntity.setChunked(true);
httpPost.setEntity(reqEntity);
HttpResponse response = httpclient.execute(httpPost);
HttpEntity resEntity = response.getEntity();
if(response.getStatusLine().getStatusCode() == 200){
success = true;
}
if (resEntity != null) {
System.out.println("Tamanho: " + resEntity.getContentLength());
System.out.println("Chunked?: " + resEntity.isChunked());
}
EntityUtils.consume(resEntity);
}
catch (Exception e) {
System.out.println(e);
}
finally {
httpclient.getConnectionManager().shutdown();
}
return success;
}
}
import org.apache.http.HttpEntity;
导入org.apache.http.HttpResponse;
导入org.apache.http.client.HttpClient;
导入org.apache.http.client.methods.HttpPost;
导入org.apache.http.entity.StringEntity;
导入org.apache.http.impl.client.DefaultHttpClient;
导入org.apache.http.util.EntityUtils;
公共类测试{
公共静态void main(字符串[]args)
{
布尔成功=XMLDataPost();
System.out.println(成功);
}
私有静态布尔XMLDataPost(){
布尔成功=假;
HttpClient HttpClient=新的DefaultHttpClient();
试一试{
HttpPost HttpPost=新的HttpPost(“http://webservice.blablabla.com.br");
StringEntity reqEntity=新StringEntity(“0256288000191159828”);
requentity.setContentType(“文本/xml”);
reqEntity.setChunked(真);
httpPost.setEntity(reqEntity);
HttpResponse response=httpclient.execute(httpPost);
HttpEntity当前性=response.getEntity();
if(response.getStatusLine().getStatusCode()==200){
成功=真实;
}
if(最近性!=null){
System.out.println(“Tamanho:+resEntity.getContentLength());
System.out.println(“Chunked?:”+resEntity.isChunked());
}
实体效用消耗(最近);
}
捕获(例外e){
系统输出打印ln(e);
}
最后{
httpclient.getConnectionManager().shutdown();
}
回归成功;
}
}
到目前为止它是如何工作的?你得到200状态码了吗?你最近有什么收获吗?