Skip to content

Instantly share code, notes, and snippets.

@RickardPettersson
Created September 28, 2011 05:49
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 RickardPettersson/1247081 to your computer and use it in GitHub Desktop.
Save RickardPettersson/1247081 to your computer and use it in GitHub Desktop.
Trafikverket Exponerar API (test kod i .Net (C#))
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