C# Post发送 接受Xml
阅读原文时间:2023年07月12日阅读:1

//组合xml内容
StringBuilder strBuilder = new StringBuilder();
var par= @" wx2421b1c4370ec43b 支付测试 H5支付测试 10000100 1add1a30ac87aa2db72f57a2375d8fec http://wxpay.wxutil.com/pub_v2/pay/notify.v2.php oUpF8uMuAJO_M2pxb1Q9zNjWeS6o 1415659990 14.23.150.211 1 MWEB {""h5_info"": {""type"":""IOS"",""app_name"": ""王者荣耀"",""package_name"": ""com.tencent.tmgp.sgame""}} 0CB01533B8C1EF103065174F50BCA001 ";
strBuilder.Append(par);
string data = strBuilder.ToString();
//进行utf-8编码
var encoding = Encoding.GetEncoding("utf-8");
byte[] buffer = encoding.GetBytes(data);
//根据webURL创建HttpWebRequest对象
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "post";
//request.Headers.Add("charset:utf-8");
request.ContentLength = buffer.Length;
request.ContentType = "text/xml";

        StreamWriter myWriter = null;  
        try  
        {  
            myWriter = new StreamWriter(request.GetRequestStream());  
            myWriter.Write(data);  
        }  
        catch (Exception e)  
        {  
            Console.WriteLine(e.Message);  
        }  
        finally  
        {  
            myWriter.Close();  
        }  
        //读取服务器返回的信息  
        HttpWebResponse objResponse = (HttpWebResponse)request.GetResponse();  
        using (StreamReader sr = new StreamReader(objResponse.GetResponseStream()))  
        {  
            string result = string.Empty;  
            result = sr.ReadToEnd();  
            Console.WriteLine(result);  
            var resultStr = XElement.Parse("result");  
            var result\_code = resultStr.Element("return\_code").Value;  
            var result\_msg = resultStr.Element("return\_msg").Value;  
        }