返回顶部

收藏

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

发表评论