Created
July 31, 2011 02:18
-
-
Save AHartTN/1116282 to your computer and use it in GitHub Desktop.
Traffic Classes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static T JsonPost<T>(string url, string strJson) | |
{ | |
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); | |
request.ContentType = "application/json; charset=utf-8"; | |
request.Accept = "application/json, text/javascript, */*"; | |
request.Method = "POST"; | |
byte[] byteArray = Encoding.UTF8.GetBytes(strJson); | |
request.ContentLength = byteArray.Length; | |
using (Stream s = request.GetRequestStream()) | |
{ | |
s.Write(byteArray, 0, byteArray.Length); | |
} | |
string json = ""; | |
WebResponse response = null; | |
try | |
{ | |
response = request.GetResponse(); | |
} | |
catch (WebException e) | |
{ | |
response = e.Response; | |
} | |
catch (Exception e) | |
{ | |
MessageBox.Show(e.Message, "An error has occurred!", MessageBoxButtons.OK, MessageBoxIcon.Error); | |
} | |
if (response != null) | |
{ | |
Stream stream = response.GetResponseStream(); | |
if (stream != null) | |
using (StreamReader sr = new StreamReader(stream)) | |
{ | |
while (!sr.EndOfStream) | |
{ | |
json += sr.ReadLine(); | |
} | |
} | |
} | |
T result = Deserialize<T>(json); | |
return result; | |
} | |
public static string Serialize(object obj) | |
{ | |
JavaScriptSerializer jss = new JavaScriptSerializer(); | |
return jss.Serialize(obj).Replace("parms", "params"); | |
} | |
public static T Deserialize<T>(string source) | |
{ | |
JavaScriptSerializer jss = new JavaScriptSerializer(); | |
T result = jss.Deserialize<T>(source); | |
return result; | |
} | |
public static dynamic DynamicDeserialize(string source) | |
{ | |
JavaScriptSerializer jss = new JavaScriptSerializer(); | |
jss.RegisterConverters(new JavaScriptConverter[] { new DynamicJsonConverter() }); | |
return jss.Deserialize(source, typeof(object)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment