I’ve been learning C# over the last few weeks, and I’m very impressed. A lot like the Objective C/Cocoa combination on the Mac, it’s focused on making GUI development very fast and easy. The Visual Studio integration is a lot slicker than Apple’s Interface Builder, without the complex, obscure and manual wiring together of components and code that IB requires. I still found myself hunting around in endless property windows for the right member or event, but touches like being able to double-click on an event name and have VS insert an empty handler function in your class really sped up my development. Combining that ease-of-use with Add-In Express’s environment for building Office and IE plugins has helped me make great progress.
One of the things I need to do a lot is communicate with a remote web server. The XMLHttpRequest JavaScript interface has become the standard for web APIs, and C# has its own version, WebRequest/HttpRequest. I’ve included an example class below that implements a synchronous POST request on top of this. To use it in your own project you’ll need to add System.Web as an external reference if you don’t already have it. It’s also possible to use HttpRequest asynchronously, but I’ve left that out of this code. The interface takes an array map of variable names and values to pass as the POST variables, and the current error logging is through a MessageBox alert, which you’ll want to change in production!
Download PeteXMLHttpRequest.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.IO;
using System.Windows.Forms;
using System.Web;
namespace MailanaOutlook
{
class PeteXMLHttpRequest
{
public static string dictionaryToPostString(Dictionary<string, string> postVariables)
{
string postString = "";
foreach (KeyValuePair<string, string> pair in postVariables)
{
postString += HttpUtility.UrlEncode(pair.Key) + "=" +
HttpUtility.UrlEncode(pair.Value) + "&";
}
return postString;
}
public static Dictionary<string, string> postStringToDictionary(string postString)
{
char[] delimiters = { ‘&’ };
string[] postPairs = postString.Split(delimiters);
Dictionary<string, string> postVariables = new Dictionary<string, string>();
foreach (string pair in postPairs)
{
char[] keyDelimiters = { ‘=’ };
string[] keyAndValue = pair.Split(keyDelimiters);
if (keyAndValue.Length > 1)
{
postVariables.Add(HttpUtility.UrlDecode(keyAndValue[0]),
HttpUtility.UrlDecode(keyAndValue[1]));
}
}
return postVariables;
}
public static string postSynchronous(string url, Dictionary<string, string> postVariables)
{
string result = null;
try
{
string postString = dictionaryToPostString(postVariables);
byte[] postBytes = Encoding.ASCII.GetBytes(postString);
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
webRequest.Method = "POST";
webRequest.ContentType = "application/x-www-form-urlencoded";
webRequest.ContentLength = postBytes.Length;
Stream postStream = webRequest.GetRequestStream();
postStream.Write(postBytes, 0, postBytes.Length);
postStream.Close();
HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse();
Console.WriteLine(webResponse.StatusCode);
Console.WriteLine(webResponse.Server);
Stream responseStream = webResponse.GetResponseStream();
StreamReader responseStreamReader = new StreamReader(responseStream);
result = responseStreamReader.ReadToEnd();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
return result;
}
}
}
new t c#.
How would this class be called from Main()
HI, which namespace is HttpUtility in? I’m using .NET 4.5.2