Skip to content

Instantly share code, notes, and snippets.

@defeated
Created August 9, 2009 15:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save defeated/164796 to your computer and use it in GitHub Desktop.
Save defeated/164796 to your computer and use it in GitHub Desktop.
private string GetTemperatureViaSOAP11() {
var results = string.Empty;
var url = "http://ws.cdyne.com/WeatherWS/Weather.asmx";
var ns = "http://ws.cdyne.com/WeatherWS/";
var soap = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>"
+ "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"
+ " <soap:Body>"
+ " <GetCityWeatherByZIP xmlns=\"{1}\">"
+ " <ZIP>{0}</ZIP>"
+ " </GetCityWeatherByZIP>"
+ " </soap:Body>"
+ "</soap:Envelope>";
var data = Encoding.UTF8.GetBytes(string.Format(soap, zipcode.Text, ns));
var web = new WebClient();
web.Headers.Add("Content-Type", "text/xml; charset=utf-8");
web.Headers.Add("SOAPAction", ns + "GetCityWeatherByZIP");
using(web) {
var bytes = web.UploadData(new Uri(url), "POST", data);
using(var stream = new MemoryStream(bytes))
using(var reader = new StreamReader(stream)) {
results = reader.ReadToEnd();
}
}
var xml = XDocument.Parse(results);
var body = xml.Root.Element(XName.Get("Body", "http://schemas.xmlsoap.org/soap/envelope/"));
var response = body.Element(XName.Get("GetCityWeatherByZIPResponse", ns));
var result = response.Element(XName.Get("GetCityWeatherByZIPResult", ns));
return result.Element(XName.Get("Temperature", ns)).Value;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment