Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.IO;
- using System.Xml;
- using System.Net;
- using System.Threading;
- namespace AssemblaIntegration
- {
- class Helper
- {
- public static String PostTicket()
- {
- String responseFromServer = String.Empty;
- try
- {
- StringWriter TicketText = new StringWriter();
- XmlTextWriter TicketRequest = new XmlTextWriter(TicketText);
- TicketRequest.WriteStartElement("ticket");
- TicketRequest.WriteElementString("summary", "fldSummary");
- TicketRequest.WriteElementString("description", "fldFeedback");
- TicketRequest.WriteElementString("priority", "3");
- TicketRequest.WriteElementString("status", "New");
- TicketRequest.WriteElementString("permission_type", "2");
- TicketRequest.WriteElementString("created_on", DateTime.Now.ToString());
- TicketRequest.WriteEndElement();
- byte[] data = Encoding.UTF8.GetBytes(TicketText.ToString());
- IWebProxy objProxy;
- Uri uri = new Uri("https://www.assembla.com/spaces/<Space ID>/tickets");
- //.Net's implementation of expectations is horribly broken,
- //it ends up sending the body twice, buffering the whole
- //thing once on the client side. This works around the problem
- ServicePointManager.MaxServicePointIdleTime = 15000;
- ServicePointManager.Expect100Continue = false;
- //Create the URL and open a connection to it for both reading
- //and writing, keeping track of cookies
- CookieContainer cookies = new CookieContainer();
- HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(uri);
- webRequest.CookieContainer = cookies;
- webRequest.Method = "POST";
- webRequest.PreAuthenticate = true;
- webRequest.AllowAutoRedirect = false;
- objProxy = WebRequest.GetSystemWebProxy();
- objProxy.Credentials = System.Net.CredentialCache.DefaultCredentials;
- webRequest.Proxy = objProxy;
- //provide authentication information
- //Unfortunately, there's yet another bug in .Net in that it
- //ignore the PreAuthenticate header on the first request
- //(true, it may not know the type, but even if you use a
- //CredentialCache and tell it the type it still doesn't use it)
- //So in addition to setting the credentials manually (the Right Thing
- //to do) we also encode the authentication manually
- webRequest.Credentials = new NetworkCredential("UserName", "Password");
- webRequest.Headers["Authorization"] =
- "Basic "
- + Convert.ToBase64String(
- Encoding.UTF8.GetBytes(
- "UserName" + ":" + "Password"));
- webRequest.Accept = "application/xml";
- //The content-type is very important
- webRequest.ContentType = "application/xml";
- webRequest.AllowWriteStreamBuffering = true;
- webRequest.SendChunked = false;
- webRequest.ContentLength = data.Length;
- using (Stream stream = webRequest.GetRequestStream())
- {
- stream.Write(data, 0, data.Length);
- }
- Thread.Sleep(1000);
- HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse();
- StreamReader reader = new StreamReader(response.GetResponseStream());
- responseFromServer = reader.ReadToEnd();
- response.Close();
- }
- catch (Exception ex)
- {
- Console.WriteLine(ex.Message);
- }
- return responseFromServer;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment