使用WebRequest Class发送XML

Hi I want to send below XML data to web server by using webrequest class. I done it successfully by posting single variable. But i am not able to include below parsed XML into it. Please help me out.

static void Main(string[] args)
    {
        using (XmlReader reader = XmlReader.Create("filepath"))
        {
            while (reader.Read())
            {
                switch (reader.NodeType)
                { 
                    case XmlNodeType.Element:
                        Console.WriteLine("Start Elemet {0}", reader.Name);
                        break;
                    case XmlNodeType.Text:
                        Console.WriteLine("Text Node: {0}", reader.Value);
                        break;
                    case XmlNodeType.EndElement:
                        Console.WriteLine("EndElement {0}", reader.Name);
                        break;
                    default:
                        Console.WriteLine("Other node {0} with value {1}",
                                        reader.NodeType, reader.Value);
                        break;
                 }
            }
        }
    }    

Below is the code which i got from msdn website and through this i want to send above XML data.

   public class WebRequestPostExample
{
    public static void Main ()
    {
        // Create a request using a URL that can receive a post. 
        WebRequest request = WebRequest.Create ("http://www.contoso.com/PostAccepter.aspx ");
        // Set the Method property of the request to POST.
        request.Method = "POST";
        // Create POST data and convert it to a byte array.
        string postData = "This is a test that posts this string to a Web server.";
        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 ();
    }
}

and i want to receiver end is in PHP.

doupi1532
doupi1532 有效。您能告诉我上面的xml解析代码如何包含在帖子数据中。
接近 6 年之前 回复
dsh12544
dsh12544 我的意思是为什么这不起作用。
接近 6 年之前 回复
doujiku1028
doujiku1028 我只想使用上面的webrequest类发送xml数据。
接近 6 年之前 回复
dscs63759
dscs63759 定义“不能”。你期望发生什么,发生了什么?
接近 6 年之前 回复

1个回答



试试这个</ p>

  XElement xml = XElement.Load(xmlFile); 

HttpWebRequest request = WebRequest.Create(new Uri(destinationUrl));

byte [] data = Encoding.Default.GetBytes(xml.Value);
request.Method =“POST”;
request.ContentType =“application /xml";nnquest.ContentLength = data.Length;

Stream sout = request.GetRequestStream();
sout.Write(data,0,data.Length);
sout.Flush();
sout.Close ();

HttpWebResponse response = request.GetResponse();
</ code> </ pre>
</ div>

展开原文

原文

try this

XElement xml=XElement.Load(xmlFile);

HttpWebRequest request = WebRequest.Create(new Uri(destinationUrl));

byte[] data = Encoding.Default.GetBytes(xml.Value);
request.Method = "POST";
request.ContentType="application/xml";
request.ContentLength = data.Length;

Stream sout = request.GetRequestStream();
sout.Write(data, 0, data.Length);
sout.Flush();
sout.Close();

HttpWebResponse response = request.GetResponse();

dqm4675
dqm4675 嗨,我没有为我工作。 它是从你的最终工作..?
接近 6 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐