PivotalTrackerAPI/Domain/Services/PivotalService.cs
/// <summary>
/// Generic method to post data to the API
/// </summary>
/// <remarks>This method does not catch errors, so the caller needs to handle any failures to communicate with Pivotal</remarks>
/// <param name="url">Url to submit to</param>
/// <param name="data">Xml data as a string</param>
/// <param name="method">The method (PUT, DELETE, POST, or GET) to use when sending data to the server</param>
/// <returns>response from Pivotal API</returns>
Generic method to post data to the API
Generic method to post data to the API
public static XmlDocument SubmitData(string url, string data, ServiceMethod method)
    {
      HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
      request.ContentType = "application/xml";
      request.Method = method.ToString();
      if (!string.IsNullOrEmpty(data))
      {
        byte[] byteArray = Encoding.UTF8.GetBytes(data);
        request.ContentLength = byteArray.Length;
        Stream dataStream = null;
        try
        {
          dataStream = request.GetRequestStream();
          dataStream.Write(byteArray, 0, byteArray.Length);
        }
        finally
        {
          if (dataStream != null)
            dataStream.Close();
        }
      }
      HttpWebResponse response = null;
      StreamReader reader = null;
      XmlDocument xmlDoc = new XmlDocument();
      try
      {
        response = request.GetResponse() as HttpWebResponse;
        reader = new StreamReader(response.GetResponseStream());
        xmlDoc.Load(reader);
      }
      finally
      {
        if (response != null)
          response.Close();
        if (reader != null)
          reader.Close();
      }
      return xmlDoc;
    }
public static XmlDocument SubmitData ( string url , string data , ServiceMethod method ) { HttpWebRequest request = ( HttpWebRequest ) WebRequest . Create ( url ) ; request . ContentType = " " ; request . Method = method . ToString ( ) ; if ( ! string . IsNullOrEmpty ( data ) ) { byte [ ] byteArray = Encoding . UTF8 . GetBytes ( data ) ; request . ContentLength = byteArray . Length ; Stream dataStream = null ; try { dataStream = request . GetRequestStream ( ) ; dataStream . Write ( byteArray , 0 , byteArray . Length ) ; } finally { if ( dataStream != null ) dataStream . Close ( ) ; } } HttpWebResponse response = null ; StreamReader reader = null ; XmlDocument xmlDoc = new XmlDocument ( ) ; try { response = request . GetResponse ( ) as HttpWebResponse ; reader = new StreamReader ( response . GetResponseStream ( ) ) ; xmlDoc . Load ( reader ) ; } finally { if ( response != null ) response . Close ( ) ; if ( reader != null ) reader . Close ( ) ; } return xmlDoc ; }
Generic method to post data to the API
Generic method to post data to the API
//request.ContentType = "application/x-www-form-urlencoded"; //request.Method = "POST";
response from Pivotal API
response from Pivotal API
url
string
Url to submit to
Url to submit to
data
string
Xml data as a string
Xml data as a string
method
ServiceMethod
The method (PUT, DELETE, POST, or GET) to use when sending data to the server
The method ( PUT DELETE POST or GET ) to use when sending data to the server
remarks
This method does not catch errors, so the caller needs to handle any failures to communicate with Pivotal
This method does not catch errors so the caller needs to handle any failures to communicate with Pivotal