Last active
February 5, 2016 16:11
-
-
Save gelias/a487a38e1902399daabc to your computer and use it in GitHub Desktop.
Sample Code using C# to integrate with uMov.me API
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
using System; | |
using System.Collections.Generic; | |
using System.IO; | |
using System.Linq; | |
using System.Net; | |
using System.Text; | |
using System.Threading.Tasks; | |
using System.Web; | |
namespace ConsoleAPI | |
{ | |
class Program | |
{ | |
const string PostMethod = "POST"; | |
const string UrlEncoded = "application/x-www-form-urlencoded"; | |
static void Main(string[] args) | |
{ | |
#region Input Data | |
String xml = "<serviceLocal><description>TESTE 3216550</description></serviceLocal>"; | |
var url = "http://api.umov.me/CenterWeb/api/{TOKEN}/serviceLocal.xml"; | |
#endregion | |
#region Calling uMov.me API | |
String xmlResult = String.Empty; | |
String requestString = HttpUtility.UrlPathEncode(String.Format("data={0}", xml)); | |
try | |
{ | |
var request = (HttpWebRequest)HttpWebRequest.Create(url); | |
request.Method = PostMethod; | |
request.ContentType = UrlEncoded; | |
StreamWriter sw = new StreamWriter(request.GetRequestStream()); | |
sw.WriteLine(requestString); | |
sw.Close(); | |
#endregion | |
#region Getting Response Message | |
var response = (HttpWebResponse)request.GetResponse(); | |
if (request != null) request.GetRequestStream().Close(); | |
string responseFromServer = string.Empty; | |
if (response != null) | |
{ | |
StreamReader incomingStreamReader = new StreamReader(response.GetResponseStream()); | |
responseFromServer = incomingStreamReader.ReadToEnd(); | |
incomingStreamReader.Close(); | |
response.GetResponseStream().Close(); | |
} | |
xmlResult = responseFromServer.ToString(); | |
#endregion | |
} | |
catch (WebException webEx) | |
{ | |
var resp = new StreamReader(webEx.Response.GetResponseStream()).ReadToEnd(); | |
Console.WriteLine(Encoding.Default.GetBytes(resp)); | |
} | |
catch (Exception ex) | |
{ | |
string errorMessage = ex.Message.ToString(); | |
Console.Out.WriteLine (errorMessage); | |
} | |
Console.Out.WriteLine (xmlResult); | |
return result; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment