zoukankan      html  css  js  c++  java
  • 发送Http请求调用webService

    如果调用WebService的不是在.NET中,无法直接添加web引用,那怎么调用webservice。

    有两种方式

    第一种方式:GET方式

      string strUrl = "http://127.0.0.1/rss/webservice.asmx/GetNews?topNum=2";
            HttpWebRequest hwrq = (HttpWebRequest)WebRequest.Create(strUrl);
            hwrq.Method = "GET";
            HttpWebResponse hwrp = (HttpWebResponse)hwrq.GetResponse();
            Stream stream = hwrp.GetResponseStream();
            XmlDocument doc = new XmlDocument();
            doc.Load(stream);
            string strLine = "";
            XmlNodeList list = doc.GetElementsByTagName("Table");
            for (int i = 0; i < list.Count; i++)
            {
                XmlNodeList chlist = list[i].ChildNodes;
                for (int j = 0; j < chlist.Count; j++)
                {
                    strLine = strLine + chlist[j].Name + "	" + chlist[j].InnerText + "
    ";
                }
                strLine = strLine + "
    ";
            }
            StreamWriter sw = new StreamWriter(@"c:/c.txt");
            sw.WriteLine(strLine);
            sw.Flush();
            sw.Close();

    第二种方式:POST方式

    string strUrl = "http://127.0.0.1/rss/webservice.asmx/GetNews";
            HttpWebRequest hwrq = (HttpWebRequest)WebRequest.Create(strUrl);
            hwrq.Method = "POST";
            hwrq.ContentType = "application/x-www-form-urlencoded";
            String param1 = HttpUtility.UrlEncode("topNum");
            param1 += "=" + HttpUtility.UrlEncode("5");
            byte[] bt = Encoding.UTF8.GetBytes(param1);
            hwrq.ContentLength = bt.Length;
            Stream sw = hwrq.GetRequestStream();
            sw.Write(bt, 0, bt.Length);
            sw.Close();
            HttpWebResponse hwrp = (HttpWebResponse)hwrq.GetResponse();
            Stream stream = hwrp.GetResponseStream();
            XmlDocument xmldoc = new XmlDocument();
            xmldoc.Load(stream);
            XmlNodeList list = xmldoc.GetElementsByTagName("Table");
            string strLine = "";
            for (int i = 0; i < list.Count; i++)
            {
                XmlNodeList chlist = list[i].ChildNodes;
                for (int j = 0; j < chlist.Count; j++)
                {
                    strLine = strLine + chlist[j].Name + "	" + chlist[j].InnerText + "
    ";
                }
                strLine = strLine + "
    ";
            }
            StreamWriter writer = new StreamWriter(@"c:c.txt");
            writer.WriteLine(strLine);
            writer.Flush();
            sw.Close();

    get方式与post方式的主要区别在于post方式的参数要经过url编码,并在获得请求之前传送。

    post的方式的参数一定要以实例中方式来写,否则会出现HTTP请求500错误。

    string param1 = HttpUtility.UrlEncode("topNum");

    param1+="="+HttpUtility.UrlEncode("5");

  • 相关阅读:
    配置python库源为清华源
    ubuntu下安装使用jupyter
    《在下雪天气里和女朋友在一起就会有种很特别的氛围,我很喜欢》梗图
    ubuntu安装cuda、cudnn和nvidia-docker
    jreg视频内容整理
    【北邮人论坛帖子备份】给考公同学的建议。如今我流的泪都是当年脑子进的水
    拉取docker容器后启动容器的脚本
    多个git账户ssh密钥配置
    【北邮人论坛帖子备份】【FAQ】给今年国企求职学弟学妹的一点建议
    nui-app记录不明白的属性
  • 原文地址:https://www.cnblogs.com/Violety/p/11917296.html
Copyright © 2011-2022 走看看