• HttpWebRequest post请求获取webservice void数据信息


            private void button2_Click(object sender, EventArgs e)
            {
                Hashtable ht = new Hashtable();
                ht["schoolid"] = "58df1006-faec-4f98-a643-2b0bfce26427";
                ht["pageindex"] = "1";
                ht["pagesize"] = "909";
                webBrowser1.DocumentText = WebSer_Post("GetStudentNotice", ht);
            }
            string url = "http://localhost:9992/CCDist2Srv.asmx";
            public string WebSer_Get(string methodName, Hashtable htParms)
            {
    
                string queryURL = string.Format("{0}/{1}?{2}", url, methodName, ParmsToString(htParms));
                HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(queryURL);
                StreamReader sr = new StreamReader(req.GetResponse().GetResponseStream(), Encoding.UTF8);
                string str = sr.ReadToEnd();
                return str;
            }
    
            public string WebSer_Post(string methodName, Hashtable htParms)
            {
                string queryURL = string.Format("{0}/{1}", url, methodName);
                HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(queryURL);
                req.Method = "POST";
                req.ContentType = "application/x-www-form-urlencoded";
                req.Timeout = 10000;
                string strpost = ParmsToString(htParms);
                byte[] bytParms = Encoding.UTF8.GetBytes(strpost);
                req.ContentLength = bytParms.Length;
                Stream sw = req.GetRequestStream();
                sw.Write(bytParms, 0, bytParms.Length);
                sw.Close();
                HttpWebResponse res;
                try
                {
                    htParms.Clear();
                    res = (HttpWebResponse)req.GetResponse();
                }
                catch (WebException ex)
                {
                    res = (HttpWebResponse)ex.Response;
                }
                StreamReader sr = new StreamReader(res.GetResponseStream(), Encoding.UTF8);
                string str = sr.ReadToEnd();
                return str;
            }
    

    C#编写的webservice,某些特殊情况可能是返回值是void类型,实际的输出数据通过

    Respose.Write();

    Reponse.End();来输出.

    这种情况获取的方法见如上代码.

    分为Get和Post.

    Soap获取的方法请参看这篇博客

    http://www.cnblogs.com/eagle1986/archive/2012/09/03/2669699.html

  • 相关阅读:
    slf4j的使用
    hashMap
    HBase
    HBase应用快速开发
    初学MongoDB 遇到提示由于目标计算机积极拒绝,无法连接
    Flask学习中运行“helloworld”出现UnicodeDecodeError: 'utf-8' codec can't decode问题
    doGet或doPost方法没有调用的一个原因
    markdown测试
    tomcat集成到IDEA与部署项目
    tomcat部署项目的方式
  • 原文地址:https://www.cnblogs.com/MarsPanda/p/5822081.html
Copyright © 2020-2023  润新知