meatbag

Assembla post ticket c#

Nov 19th, 2013
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.19 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.IO;
  6. using System.Xml;
  7. using System.Net;
  8. using System.Threading;
  9.  
  10. namespace AssemblaIntegration
  11. {
  12.     class Helper
  13.     {
  14.         public static String PostTicket()
  15.         {
  16.             String responseFromServer = String.Empty;
  17.             try
  18.             {
  19.                 StringWriter TicketText = new StringWriter();
  20.                 XmlTextWriter TicketRequest = new XmlTextWriter(TicketText);
  21.  
  22.                 TicketRequest.WriteStartElement("ticket");                
  23.                 TicketRequest.WriteElementString("summary", "fldSummary");              
  24.                 TicketRequest.WriteElementString("description", "fldFeedback");            
  25.                 TicketRequest.WriteElementString("priority", "3");              
  26.                 TicketRequest.WriteElementString("status", "New");
  27.                 TicketRequest.WriteElementString("permission_type", "2");
  28.                 TicketRequest.WriteElementString("created_on", DateTime.Now.ToString());          
  29.                 TicketRequest.WriteEndElement();
  30.  
  31.  
  32.                 byte[] data = Encoding.UTF8.GetBytes(TicketText.ToString());
  33.                 IWebProxy objProxy;              
  34.                 Uri uri = new Uri("https://www.assembla.com/spaces/<Space ID>/tickets");
  35.  
  36.                 //.Net's implementation of expectations is horribly broken,
  37.                 //it ends up sending the body twice, buffering the whole
  38.                 //thing once on the client side. This works around the problem
  39.                 ServicePointManager.MaxServicePointIdleTime = 15000;
  40.                 ServicePointManager.Expect100Continue = false;
  41.  
  42.                 //Create the URL and open a connection to it for both reading
  43.                 //and writing, keeping track of cookies
  44.                 CookieContainer cookies = new CookieContainer();
  45.                 HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(uri);
  46.                 webRequest.CookieContainer = cookies;
  47.                 webRequest.Method = "POST";
  48.                 webRequest.PreAuthenticate = true;
  49.                 webRequest.AllowAutoRedirect = false;
  50.  
  51.  
  52.                 objProxy = WebRequest.GetSystemWebProxy();
  53.                 objProxy.Credentials = System.Net.CredentialCache.DefaultCredentials;
  54.                 webRequest.Proxy = objProxy;
  55.  
  56.                 //provide authentication information
  57.                 //Unfortunately, there's yet another bug in .Net in that it
  58.                 //ignore the PreAuthenticate header on the first request
  59.                 //(true, it may not know the type, but even if you use a
  60.                 //CredentialCache and tell it the type it still doesn't use it)
  61.                 //So in addition to setting the credentials manually (the Right Thing
  62.                 //to do) we also encode the authentication manually
  63.                 webRequest.Credentials = new NetworkCredential("UserName", "Password");
  64.  
  65.                 webRequest.Headers["Authorization"] =
  66.                     "Basic "
  67.                     + Convert.ToBase64String(
  68.                         Encoding.UTF8.GetBytes(
  69.                             "UserName" + ":" + "Password"));
  70.  
  71.                 webRequest.Accept = "application/xml";
  72.                 //The content-type is very important
  73.                 webRequest.ContentType = "application/xml";
  74.                 webRequest.AllowWriteStreamBuffering = true;
  75.                 webRequest.SendChunked = false;
  76.                 webRequest.ContentLength = data.Length;
  77.                 using (Stream stream = webRequest.GetRequestStream())
  78.                 {
  79.                     stream.Write(data, 0, data.Length);
  80.                 }
  81.  
  82.                 Thread.Sleep(1000);
  83.                 HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse();
  84.                 StreamReader reader = new StreamReader(response.GetResponseStream());
  85.                 responseFromServer = reader.ReadToEnd();
  86.                 response.Close();
  87.             }
  88.             catch (Exception ex)
  89.             {
  90.                 Console.WriteLine(ex.Message);
  91.             }
  92.             return responseFromServer;
  93.         }
  94.     }
  95. }
Advertisement
Add Comment
Please, Sign In to add comment