Venky Dirisala
Venky Dirisala

Reputation: 11

Apache HTTP Client 400 Error

I was Trying to Automate a Web Service and I am passing the XML in the form of a String and later converting it to String Entity and Setting the Entity. But I don't know why it is throwing 400 Error. I am new to WebServices Automation could any please help me on this.

Below is my Code:

package com.WebServices.Automation;

import java.io.IOException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
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.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.junit.Assert;
import org.junit.Test;

public class HTTPClientA {

    static String url = "http://www.dneonline.com/calculator.asmx?wsdl";

    String xml = "\"<?xml version=\"1.0\" encoding=\"UTF-8\"?>"+
            "<soap:Envelope xmlns:soap=\"http://www.w3.org/2003/05/soap-envelope\" xmlns:tem=\"http://tempuri.org/\">\r\n" + 
            "   <soap:Header/>\r\n" + 
            "   <soap:Body>\r\n" + 
            "      <tem:Add>\r\n" + 
            "         <tem:intA>10</tem:intA>\r\n" + 
            "         <tem:intB>20</tem:intB>\r\n" + 
            "      </tem:Add>\r\n" + 
            "   </soap:Body>\r\n" + 
            "</soap:Envelope>";
    @Test
    public void main() throws ClientProtocolException, IOException
    {

        StringEntity stringEntity = new StringEntity(xml);
        HttpPost post = new HttpPost(url);
        post.setEntity(stringEntity);
        HttpClient client = HttpClientBuilder.create().build();
        post.setHeader("Content-Type", "text/xml; charset=utf-8");
        post.setHeader("SOAPAction", "http://tempuri.org/Add");
        HttpResponse res = client.execute(post);

        int actualresponse = res.getStatusLine().getStatusCode();
        System.out.println(actualresponse);
        try
        {
        Assert.assertEquals(actualresponse, 200);
        }
        catch (Exception e) {
            // TODO: handle exception
        } 
        HttpEntity entity = res.getEntity();

            String strResponse = null;
            if (entity != null) {
                strResponse = EntityUtils.toString(entity);
                System.out.println(strResponse);
            }
    }

}

Upvotes: 0

Views: 662

Answers (1)

Ori Marko
Ori Marko

Reputation: 58862

Your XML is invalid, it start with double quote instead of <?xml, change your assignment to start with:

  String xml = "<?xml version ... –

Upvotes: 1

Related Questions