Created
September 28, 2011 05:49
-
-
Save RickardPettersson/1247081 to your computer and use it in GitHub Desktop.
Trafikverket Exponerar API (test kod i .Net (C#))
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
HttpWebRequest req = null; | |
HttpWebResponse rsp = null; | |
try | |
{ | |
string uri = "http://www.trafikverket.se/Trafikverket/Orion/OrionProxy.ashx"; | |
req = (HttpWebRequest)HttpWebRequest.Create(uri); | |
req.Method = "POST"; // Post method | |
req.ContentType = "text/xml; encoding='utf-8'"; // content type | |
byte[] bytes = System.Text.Encoding.ASCII.GetBytes(@"<ORIONML version='1.0'> | |
<REQUEST plugin='KartDB'> | |
<PLUGINML table='Stations'></PLUGINML> | |
</REQUEST> | |
</ORIONML>"); | |
req.ContentLength = bytes.Length; | |
Stream writer = req.GetRequestStream(); | |
writer.Write(bytes, 0, bytes.Length); | |
writer.Close(); | |
// Send the data to the webserver | |
rsp = (HttpWebResponse)req.GetResponse(); | |
if (rsp.StatusCode == HttpStatusCode.OK) | |
{ | |
//Get response stream | |
Stream objResponseStream = rsp.GetResponseStream(); | |
//Load response stream into XMLReader | |
XmlTextReader objXMLReader = new XmlTextReader(objResponseStream); | |
//Declare XMLDocument | |
XmlDocument xmldoc = new XmlDocument(); | |
xmldoc.Load(objXMLReader); | |
Console.WriteLine(xmldoc.OuterXml); | |
//Close XMLReader | |
objXMLReader.Close(); | |
} | |
} | |
catch (WebException webEx) | |
{ | |
Console.WriteLine("HttpPost - WebException: " + webEx.ToString()); | |
} | |
catch (Exception ex) | |
{ | |
Console.WriteLine("HttpPost - Exception: " + ex.ToString()); | |
} | |
finally | |
{ | |
if (req != null) req.GetRequestStream().Close(); | |
if (rsp != null) rsp.GetResponseStream().Close(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment