C# 使用ApacheHttpComponents发送xml

C# 使用ApacheHttpComponents发送xml,c#,java,apache-httpcomponents,C#,Java,Apache Httpcomponents,我正试图用Java复制以下C#代码。这段代码是一个助手类,它发送包含xml的请求,并读取响应 internal static String Send(String url, String body) { HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url); try { // create the new httpwebrequ

我正试图用Java复制以下C#代码。这段代码是一个助手类,它发送包含xml的请求,并读取响应

    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;
                }
在阅读了其他线程之后,我确定ApacheHttpComponents库是获得相同功能的最佳选择。阅读文档并遵循此处的示例后:

我无法理解如何将正文字符串作为xml发送。当我试图为请求设置实体时,它要求我声明一个BasicNameValuePair,我不知道这是什么,也不知道如何格式化正文字符串以满足此规范。 下面是我目前所做的

    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;
}
编辑:还是应该使用StringEntity并执行以下操作

    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;
}
使用文件实体

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

这里有很多很好的例子:

因为我的xml当前是作为字符串传递的,所以使用StringEntity编辑是否更好?