现在的位置: 首页 > 综合 > 正文

C# 后台模拟Post Xml数据 并接受

2013年12月03日 ⁄ 综合 ⁄ 共 757字 ⁄ 字号 评论关闭

发送端:

//xmlDstUrl:发送的Url
//xmlContent:发送内容
                 try
                {
                    HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(xmlDstUrl);
                    byte[] requestContent = System.Text.Encoding.ASCII.GetBytes(xmlContent);

                    myRequest.Method = "POST";
                    myRequest.ContentType = "text/xml";
                    myRequest.ContentLength = requestContent.Length;
                    System.IO.Stream requestStream = myRequest.GetRequestStream();
                    requestStream.Write(requestContent, 0, requestContent.Length);
                    requestStream.Close();

                    isSuccess = true;
                }
                catch (System.Exception ex)
                {
                    isSuccess = false;
                }

接收端:
        if (Request.InputStream.Length > 0)
        {
            byte[] bytes = new byte[Request.InputStream.Length];
            Request.InputStream.Read(bytes, 0, bytes.Length);
            FileStream fs = new FileStream(Request.PhysicalApplicationPath + "File.txt", FileMode.Create);
            fs.Write(bytes, 0, bytes.Length);
            fs.Flush();
            fs.Close();
        }

抱歉!评论已关闭.