返回顶部

收藏

C#调用web service返回字符串

更多
public string getServiceResult(string serviceUrl) {
    HttpWebRequest HttpWReq;
    HttpWebResponse HttpWResp;
    HttpWReq = (HttpWebRequest)WebRequest.Create(serviceUrl);
    HttpWReq.Method = "GET";
    HttpWResp = (HttpWebResponse)HttpWReq.GetResponse();
    if (HttpWResp.StatusCode == HttpStatusCode.OK)
    {
        //Consume webservice with basic XML reading, assumes it returns (one) string
        XmlReader reader = XmlReader.Create(HttpWResp.GetResponseStream());
        while (reader.Read())
        {
            reader.MoveToFirstAttribute();
            if (reader.NodeType == XmlNodeType.Text)
            {
                return reader.Value;
            }
        }
        return String.Empty;
    }
    else
    {
        throw new Exception("Error on remote IP to Country service: "+ HttpWResp.StatusCode.ToString());
    }
}

标签:C#,WebService,http

收藏

0人收藏

支持

0

反对

0

»更多 您可能感兴趣的代码
  1. 2012-12-30 15:58:40C#动态调用WebService方法 by 吴红军
  2. 2013-11-17 09:34:41C#检查字符串是否是合法的HTTP URL地址 by xfk
  3. 2012-11-03 21:21:45C# mysql类 by 小项
  4. 2012-11-10 10:55:58Ruby 下载图片代码 by jzzlee
  5. 2012-11-17 23:23:28Javascript 调用WebMethod 举例 by JoanneTou
  6. 2012-11-20 20:31:18C#连接Access数据库的代码片段 by 灵剑子
  7. 2012-11-23 20:52:23c# 计算文件MD5值 by fengsweat
  8. 2012-11-27 10:57:04c#获得服务器的外网ip,内网ip by 西国の海妖
  9. 2012-12-04 09:20:07C#常用正则表达式整理 by digua
  10. 2012-12-13 13:35:26C#生成条形码代码 by arden
  11. 2016-12-28 20:35:28range and tuple by 大猪

发表评论