在C#HTTP发布XML数据 [英] HTTP post XML data in C#

查看:140
本文介绍了在C#HTTP发布XML数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要交的XML数据HTTP到与XMLDATA的名称textarea的一个网址。我的XML数据已经准备好,是的XDocument Sendingxml = XML内;但我已经尝试了后置代号是行不通的。主要是因为我不知道如何把XML数据POSTDATA变量,因为它只能接受字符串。这是我的代码:

 的XDocument Sendingxml = XML; 

//字符串Sendingxml =< XML版本= 1.0编码= UTF-8>< PluginData><凭证用户名= avosfieldagent01 AuthenticationToken = 61cc3957744742dca238c4dd7cbca702 /><会话>< PropertyAddress> 5 Crosskey< / PropertyAddress>< PropertyAddress2 />< PropertyCity>加利福尼亚州和LT; / PropertyCity>< PropertyState> CA< / PropertyState>< PropertyZip> 92620< / PropertyZip><属性类型>公寓< /属性类型>< ; SourceReferenceId> 45643< / SourceReferenceId><的SessionID> 2013070100158346< / SessionID的>< SessionCompleteReturnURL /><客户ID />< BluebookOrderCheckSum> 681a598cf23f412095f6092c281823e6< / BluebookOrderCheckSum>< BluebookOrderId> 11160< / BluebookOrderId>< /会议>< / PluginData>中;

//使用,它可以接收后的URL的请求。
的WebRequest请求=
WebRequest.Create(https://test.bluebookcva.net/order/testdirectplugin/3);
//设置请求POST的Method属性。
request.Method =POST;

//创建POST数据并将其转换为一个字节数组。
串POSTDATA =XMLDATA =+ Sendingxml; //我的问题就在这里,因为我需要POSTDATA为的XDocument。


字节[]的字节数组= Encoding.UTF8.GetBytes(POSTDATA);
//设置的WebRequest的ContentType属性。
request.ContentType =应用/的X WWW的形式,进行了urlencoded
//设置的WebRequest的CONTENTLENGTH财产。
request.ContentLength = byteArray.Length;
//获取请求流。
流数据流= request.GetRequestStream();
//将数据写入请求流。
dataStream.Write(字节数组,0,byteArray.Length);
//关闭Stream对象。
dataStream.Close();
//得到响应。
WebResponse的响应= request.GetResponse();
//显示状态。
Console.WriteLine(((HttpWebResponse)响应).StatusDescription);
//获取服务器返回包含流内容。
数据流= response.GetResponseStream();
//打开使用容易获得一个StreamReader流。
StreamReader的读者=新的StreamReader(数据流);
//读取内容。
串responseFromServer = reader.ReadToEnd();
//显示的内容。
Console.WriteLine(responseFromServer);
//清理流。
reader.Close();
dataStream.Close();
response.Close();


解决方案

一般来说:



一个简单的方法来发布XML数据,并获得响应(作为一个字符串)将下列函数的一个例子:

 公共字符串postXMLData(字符串目标网址,串requestXml)
{
HttpWebRequest的要求=(HttpWebRequest的)WebRequest.Create(目标网址);
字节[]字节;
字节= System.Text.Encoding.ASCII.GetBytes(requestXml);
request.ContentType =文本/ XML;编码='UTF-8';
request.ContentLength = bytes.Length;
request.Method =POST;
流requestStream = request.GetRequestStream();
requestStream.Write(字节,0,bytes.Length);
requestStream.Close();
HttpWebResponse响应;
响应=(HttpWebResponse)request.GetResponse();
如果(response.StatusCode == HttpStatusCode.OK)
{
流responseStream = response.GetResponseStream();
串responseStr =新的StreamReader(responseStream).ReadToEnd();
返回responseStr;
}
返回NULL;
}



在您的具体情况:



而不是:

  request.ContentType =应用/的X WWW的形式-urlencoded; 

使用:

  request.ContentType =文本/ XML;编码='UTF-8'; 



此外,删除:

 字符串POSTDATA =XMLDATA =+ Sendingxml; 

和替换:

 字节[]的字节数组= Encoding.UTF8.GetBytes(POSTDATA); 





 字节[]的字节数组= Encoding.UTF8.GetBytes(Sendingxml.ToString()); 


I need to HTTP post XML data to a URL that has Textarea with the name of XMLdata. My XMl data is ready and is inside of XDocument Sendingxml = xml; but the post code that I have tried is not working. Mostly because I don't know how to put the XML data in postData Variable as it only accept string. This is my code:

        XDocument Sendingxml = xml;

        //  string Sendingxml = "<?xml version=1.0 encoding=UTF-8> <PluginData> <Credential UserName=avosfieldagent01 AuthenticationToken=61cc3957744742dca238c4dd7cbca702 /><Session><PropertyAddress>5 Crosskey</PropertyAddress><PropertyAddress2/><PropertyCity>California</PropertyCity><PropertyState>CA</PropertyState><PropertyZip>92620</PropertyZip><PropertyType>Condo</PropertyType><SourceReferenceId>45643</SourceReferenceId><SessionId>2013070100158346</SessionId><SessionCompleteReturnURL/><CustomerId/><BluebookOrderCheckSum>681a598cf23f412095f6092c281823e6</BluebookOrderCheckSum><BluebookOrderId>11160</BluebookOrderId> </Session></PluginData>";

        // Create a request using a URL that can receive a post. 
        WebRequest request =
            WebRequest.Create("https://test.bluebookcva.net/order/testdirectplugin/3");
        // Set the Method property of the request to POST.
        request.Method = "POST";

        // Create POST data and convert it to a byte array.
        string postData = "XMLData=" + Sendingxml;    //My problem is here as I need postData  as XDocument.


        byte[] byteArray = Encoding.UTF8.GetBytes(postData);
        // Set the ContentType property of the WebRequest.
        request.ContentType = "application/x-www-form-urlencoded";
        // Set the ContentLength property of the WebRequest.
        request.ContentLength = byteArray.Length;
        // Get the request stream.
        Stream dataStream = request.GetRequestStream();
        // Write the data to the request stream.
        dataStream.Write(byteArray, 0, byteArray.Length);
        // Close the Stream object.
        dataStream.Close();
        // Get the response.
        WebResponse response = request.GetResponse();
        // Display the status.
        Console.WriteLine(((HttpWebResponse)response).StatusDescription);
        // Get the stream containing content returned by the server.
        dataStream = response.GetResponseStream();
        // Open the stream using a StreamReader for easy access.
        StreamReader reader = new StreamReader(dataStream);
        // Read the content.
        string responseFromServer = reader.ReadToEnd();
        // Display the content.
        Console.WriteLine(responseFromServer);
        // Clean up the streams.
        reader.Close();
        dataStream.Close();
        response.Close();

解决方案

In General:

An example of an easy way to post XML data and get the response (as a string) would be the following function:

public string postXMLData(string destinationUrl, string requestXml)
{
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(destinationUrl);
    byte[] bytes;
    bytes = System.Text.Encoding.ASCII.GetBytes(requestXml);
    request.ContentType = "text/xml; encoding='utf-8'";
    request.ContentLength = bytes.Length;
    request.Method = "POST";
    Stream requestStream = request.GetRequestStream();
    requestStream.Write(bytes, 0, bytes.Length);
    requestStream.Close();
    HttpWebResponse response;
    response = (HttpWebResponse)request.GetResponse();
    if (response.StatusCode == HttpStatusCode.OK)
    {
        Stream responseStream = response.GetResponseStream();
        string responseStr = new StreamReader(responseStream).ReadToEnd();
        return responseStr;
    }
    return null;
}

In your specific situation:

Instead of:

request.ContentType = "application/x-www-form-urlencoded";

use:

request.ContentType = "text/xml; encoding='utf-8'";

Also, remove:

string postData = "XMLData=" + Sendingxml;

And replace:

byte[] byteArray = Encoding.UTF8.GetBytes(postData);

with:

byte[] byteArray = Encoding.UTF8.GetBytes(Sendingxml.ToString());

这篇关于在C#HTTP发布XML数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆