0

I have my Rest based Service and i have to pass XML to it and when i run in RestClient ans postman it is running correctly but when i run it in Code its giving me the 400 Bad Request code is given below any sort of help would be helpfull. Thanks

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/\">\r\n   <soapenv:Header/>\r\n   <soapenv:Body>\r\n      <tem:accountbalance>\r\n         <!--Optional:-->\r\n         <tem:accountno>051000207960141</tem:accountno>\r\n      </tem:accountbalance>\r\n   </soapenv:Body>\r\n</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();
    }
}
Waseem Saeed
  • 85
  • 1
  • 13

1 Answers1

0

You need to allow the cross domain requests on the target server where your api resides. See this:- How to enable cross-domain request on the server?

Community
  • 1
  • 1
Nikhil Mahajan
  • 523
  • 5
  • 17