1

我需要向未使用 SOAP 协议开发的 Web 服务发送 XML 请求。Web 服务仅适用于纯 XML 请求/应答,因此没有 WSDL。网络服务将回答我必须下载的 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;
}
}
4

0 回答 0