Question

I am attempting to replicate the following C# code in Java. This code is a helper class that sends a request containing xml, and reads a response.

    internal static String Send(String url, String body)
    {
        HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
        try
        {
            // create the new httpwebrequest with the uri
            request.ContentLength = 0;
            // set the method to POST
            request.Method = "POST";

            if (!String.IsNullOrEmpty(body))
            {
                request.ContentType = "application/xml; charset=utf-8";
                byte[] postData = Encoding.Default.GetBytes(body);
                request.ContentLength = postData.Length;
                using (Stream s = request.GetRequestStream())
                {
                    s.Write(postData, 0, postData.Length);
                }

            }

            using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
            {
                String responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
                if (response.StatusCode != HttpStatusCode.OK)
                {
                    throw new ResponseException(((int)response.StatusCode),
                        response.StatusCode.ToString(), request.RequestUri.ToString(),
                        responseString);
                }
                return responseString;
            }
        }
        catch (WebException e)
        {
            using (WebResponse response = e.Response)
            {
                HttpWebResponse httpResponse = response as HttpWebResponse;
                if (httpResponse != null)
                {
                    using (Stream data = response.GetResponseStream())
                    {
                        data.Position = 0;
                        throw new ResponseException(((int)httpResponse.StatusCode),
                                httpResponse.StatusCode.ToString(), request.RequestUri.ToString(),
                                new StreamReader(data).ReadToEnd()
                            );
                    }
                }
                else
                {
                    throw;
                }

After reading other threads I determined that the Apache HttpComponents library would be my best bet to get the same functionality. After reading the documentation and following the example here:

http://hc.apache.org/httpcomponents-client-ga/quickstart.html

I am unable to figure out how to send the body string as xml. When I attempt to set the entity for the request it requires that I declare a BasicNameValuePair, and I do not understand what this is, or how I would format the body string to meet this specification. Below is what I have currently done.

    protected static String Send(String url, String body)
{
    HttpPost request = new HttpPost(url);

    try
    {
        request.setHeader("ContentType", "application/xml; charset=utf=8");

        // Encode the body if needed
        request.setEntity(new UrlEncodedFormEntity());

        //get the response

        // if the response code is not valid throw a ResponseException

        // else return the response string. 

    } finally {
        request.releaseConnection();
    }
    return null;
}

EDIT : or should I use a StringEntity and do the following

    protected static String SendToJetstream(String url, String body)
{
    HttpPost request = new HttpPost(url);

    try
    {
        StringEntity myEntity = new StringEntity(body, 
                ContentType.create("application/xml", "UTF-8"));


        // Encode the body if needed
        request.setEntity(myEntity);

        //get the response

        // if the response code is not valid throw a ResponseException

        // else return the response string. 

    } finally {
        request.releaseConnection();
    }
    return null;
}
Was it helpful?

Solution

Use a FileEntity

File file = new File("somefile.xml");
FileEntity entity = new FileEntity(file, ContentType.create("application/xml", "UTF-8"));

Lots of good examples here: http://hc.apache.org/httpcomponents-client-ga/tutorial/html/fundamentals.html#d5e165

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top