MVC HttpWebRequest XML 请求

前端之家收集整理的这篇文章主要介绍了MVC HttpWebRequest XML 请求前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

MVCHttpWebRequest XML类型请求解析数据@H_403_2@


public Dictionary<string,object> HttpWebRequestXML()
{
    try
    {
        string date = DateTime.Now.ToString("yyyyMMddhhmmss");

        // 构建URL内容
        string sigstr = "";
        string uriStr = string.Format("https://{0}:{1}/{2}/SubAccounts/{3}/Calls/Callback?sig={4}","m_restAddress","",sigstr);
        Uri address = new Uri(uriStr);

        // 创建网络请求  
        HttpWebRequest request = WebRequest.Create(address) as HttpWebRequest;

        // 构建Head
        request.Method = "POST";

        Encoding myEncoding = Encoding.GetEncoding("utf-8");
        byte[] myByte = myEncoding.GetBytes("123" + ":" + date);
        string authStr = Convert.ToBase64String(myByte);
        request.Headers.Add("Authorization",authStr);

        // 构建Body
        StringBuilder data = new StringBuilder();

        request.Accept = "application/xml";
        request.ContentType = "application/xml;charset=utf-8";

        data.Append("<?xml version='1.0' encoding='utf-8'?><CallBack>");
        data.Append("<from>").Append("from").Append("</from>");
        data.Append("<to>").Append("to").Append("</to>");
        data.Append("<customerSerNum>").Append("customerSerNum").Append("</customerSerNum>");
        data.Append("<fromSerNum>").Append("fromSerNum").Append("</fromSerNum>");
        data.Append("<promptTone>").Append("promptTone").Append("</promptTone>");
        data.Append("<maxCallTime>").Append("maxCallTime").Append("</maxCallTime>");
        data.Append("<hangupCdrUrl>").Append("hangupCdrUrl").Append("</hangupCdrUrl>");
        data.Append("</CallBack>");

        byte[] byteData = UTF8Encoding.UTF8.GetBytes(data.ToString());

        // 开始请求
        using (Stream postStream = request.GetRequestStream())
        {
            postStream.Write(byteData,byteData.Length);
        }

        // 获取请求
        using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
        {
            // Get the response stream  
            StreamReader reader = new StreamReader(response.GetResponseStream());
            string responseStr = reader.ReadToEnd();

            if (responseStr != null && responseStr.Length > 0)
            {
                Dictionary<string,object> responseResult = new Dictionary<string,object> { { "statusCode","0" },{ "statusMsg","成功" },{ "data",null } };

                XmlDocument resultXml = new XmlDocument();
                resultXml.LoadXml(responseStr);
                XmlNodeList nodeList = resultXml.SelectSingleNode("Response").ChildNodes;
                foreach (XmlNode item in nodeList)
                {
                    if (item.Name == "statusCode")
                    {
                        responseResult["statusCode"] = item.InnerText;
                    }
                    else if (item.Name == "statusMsg")
                    {
                        responseResult["statusMsg"] = item.InnerText;
                    }
                    else if (item.Name == "CallBack")
                    {
                        Dictionary<string,object> retData = new Dictionary<string,object>();
                        foreach (XmlNode subItem in item.ChildNodes)
                        {
                            retData.Add(subItem.Name,subItem.InnerText);
                        }
                        responseResult["data"] = new Dictionary<string,object> { { item.Name,retData } };
                    }
                }

                return responseResult;
            }
            return new Dictionary<string,172002 },"无返回" },null } };
        }
    }
    catch (Exception e)
    {
        throw e;
    }
}

猜你在找的XML相关文章