响应得到HeaderTransfer-Encoding:在Java中使用Httpclient进行分块

时间:2017-09-25 12:02:40

标签: java soap

我使用Httpclient调用Java中的Web服务。我已经从SOAP UI复制并粘贴为请求XML的请求。在SOAP UI中,我能够获得响应,但在我的代码级别,它并没有。它抛出

Reponse HeaderTransfer-Encoding: chunked
Reponse HeaderDate: Mon, 25 Sep 2017 11:54:06 GMT
Reponse Header END...
Negative array size
java.lang.NullPointerException
at java.io.ByteArrayInputStream.<init>(Unknown Source)

这是我的代码

HttpParams httpParameters = new BasicHttpParams();
                        int timeoutConnection = 15000;
                        HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
                        int timeoutSocket = 35000;
                        HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);

                        @SuppressWarnings("resource")
                        DefaultHttpClient httpclient = new DefaultHttpClient(httpParameters);


                        HttpPost httppost = new HttpPost("<SOAP End point>");
                        httppost.setHeader("soapaction", "<Action>");
                        httppost.setHeader("Content-Type", "text/xml; charset=utf-8");

                        final StringBuffer soap = new StringBuffer();
                        soap.append("<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:web=\"http://webservices.bankfusion.trapedza.com\" xmlns:att=\"http://www.misys.com/bankfusion/attributes\" xmlns:head=\"http://www.misys.com/eqf/types/header\">");
// and so on for soap appending

                        System.out.println(soap);

                        try {
                            HttpEntity entity = new StringEntity(soap.toString(),HTTP.UTF_8);
                            httppost.setEntity(entity); 
                            HttpResponse response = httpclient.execute(httppost);// calling server\
                            System.out.println("Response:"+response);
                            HttpEntity r_entity = response.getEntity();  //get response
                            System.out.println("Reponse Header Begin...");          // response headers
                            System.out.println("StatusLine:"+response.getStatusLine());
                            Header[] headers = response.getAllHeaders();
                            for(Header h:headers){
                                System.out.println("Reponse Header"+h.getName() + ": " + h.getValue());
                            }
                            System.out.println("Reponse Header END...");
                            if (r_entity != null) {       
                                if((int) r_entity.getContentLength() > 0) {
                                    result = new byte[(int) r_entity.getContentLength()];  
                                    if (r_entity.isStreaming()) {
                                        DataInputStream is = new DataInputStream(
                                                r_entity.getContent());
                                        is.readFully(result);
                                    }   
                                } else {
                                    System.out.println("Negative array size");
                                }

                            }
                        } catch (Exception E) {
                            System.out.println("Exception While Connecting");
                            E.printStackTrace();
                        }

这可能是什么问题?我该怎么解决呢?

非常感谢任何想法

0 个答案:

没有答案