Return to Snippet

Revision: 14361
at June 1, 2009 16:56 by jasonseney


Updated Code
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());
	}
}

Revision: 14360
at June 1, 2009 16:49 by jasonseney


Initial Code
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)
	{
		string result = String.Empty;

		//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)
			{
				result = reader.Value;
			}
		}
		return result;
	}
	else
	{
		throw new Exception("Error on remote IP to Country service: "+ HttpWResp.StatusCode.ToString());
	}
}

Initial URL


Initial Description
Basic example of calling a web service from code using GET. You can pass parameters in as query vars.

Assumes return type of service is "String". Example return xml:

    <?xml>
    <string>US</string>

@SNIPPLR TEAM: Please let me put xml attributes in code sections in comments!

Initial Title
C# Call Web Service and Get String Result

Initial Tags
c

Initial Language
C#