如何使用httpclient发送XML请求并接收文件? (JAVA)

时间:2013-09-15 17:40:06

标签: web-services xmlhttprequest httpclient

我需要向未使用SOAP protocoal开发的Web服务发送XML请求。 webservice仅适用于纯XML请求/回答,因此没有WSDL。 web服务将回复我必须下载的gzip文件。有人可以帮我吗?我从下面的代码开始。谢谢!

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;
}
}

0 个答案:

没有答案