Cybercrux

Everything is achievable through technology

Call rest api in c#

private RootObject Sentiment(List sentimentData)
{
RootObject sentiments = new RootObject();
string data = Newtonsoft.Json.JsonConvert.SerializeObject(sentimentData, Newtonsoft.Json.Formatting.Indented);
data = "{data:" + data + "}";
//System.Net.HttpWebRequest request = System.Net.WebRequest.Create("http://www.sentiment140.com/api/bulkClassifyJson?appid=subheesho@gmail.com") as System.Net.HttpWebRequest;
System.Net.HttpWebRequest request = System.Net.WebRequest.Create("http://www.sentiment140.com/api/bulkClassifyJson?appid="+ConfigurationManager.AppSettings["SentimentAppId"]) as System.Net.HttpWebRequest;
request.Method = "POST";
request.ContentType = "application/json"; // "application/json";
Byte[] bt = System.Text.Encoding.UTF8.GetBytes(data);
Stream st = request.GetRequestStream(); st.Write(bt, 0, bt.Length);
st.Close();
using (System.Net.HttpWebResponse response = request.GetResponse() as System.Net.HttpWebResponse)
{
if (response.StatusCode != System.Net.HttpStatusCode.OK)
throw new Exception(String.Format(
"Server error (HTTP {0}: {1}).", response.StatusCode,
response.StatusDescription));
Stream stream1 = response.GetResponseStream();
StreamReader sr = new StreamReader(stream1);
string strsb = sr.ReadToEnd();
sentiments = (RootObject)Newtonsoft.Json.JsonConvert.DeserializeObject(strsb, typeof(RootObject));
}
return sentiments;
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s