Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.