All pastes #1827416 Raw Edit

Manish Sinha

public text v1 · immutable
#1827416 ·published 2010-03-07 19:09 UTC
rendered paste body
using System;
using System.Net;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.IO;
using System.Security.Cryptography.X509Certificates;
using System.Text;

namespace LaunchpadSharp.ApiHacking
{
	class MainClass :ICertificatePolicy
	{
		public bool CheckValidationResult (ServicePoint sp, 
		X509Certificate certificate, WebRequest request, int error)
		{
			return true;
		}

		
		public static void Main(string[] args)
		{
			// Connect to edge.launchpad.net
			string baseAddress = "https://edge.launchpad.net/+request-token";
			
			#region OAuth headers
			string oauth_consumer_key = "lpSharp";
			string oauth_signature_method = "PLAINTEXT";
			string oauth_signature = "&";
			#endregion
			
			HttpWebRequest clnt = HttpWebRequest.Create(baseAddress) as HttpWebRequest;
			
			#region Set the HTTP Connection Info
			clnt.ContentType =  "application/x-www-form-urlencoded";
			clnt.Method = "POST";
			#endregion
			
			string[] listOfData =	{
									"oauth_consumer_key="+oauth_consumer_key,
									"oauth_signature="+oauth_signature,
									"oauth_signature_method"+oauth_signature_method
									};
			
			string postData = string.Join("&",listOfData);
			byte[] dataBytes= Encoding.ASCII.GetBytes(postData);
			
			
			#region Request Part
			Stream newStream = clnt.GetRequestStream();
			newStream.Write(dataBytes,0,dataBytes.Length);
			
			newStream.Close();
			#endregion
			
			#region Response Part
			
			try
	    	{
				HttpWebResponse resp = clnt.GetResponse() as HttpWebResponse;
				if (resp == null)
					Console.WriteLine("No Response");
		
				// expected response is a 200 
				if (int.Parse(resp.StatusCode.ToString()) != 200)
					Console.WriteLine(String.Format("unexpected status code ({0})", resp.StatusCode));
				for(int i=0; i < resp.Headers.Count; ++i)  
					Console.WriteLine(resp.Headers[i]);
		
				var MyStreamReader = new System.IO.StreamReader(resp.GetResponseStream());
				string fullResponse = MyStreamReader.ReadToEnd().Trim();
				Console.WriteLine("===Full Response===\n"+fullResponse);
			}
			catch (Exception ex1)
			{
				// handle 404, 503, etc...here
			}
			#endregion
			
		}
	}
}