void CallPostMethod(JArray jarray)
{
// Restful service URL
/*string url = */
//consuming third party service need to send data in json and retrieve rsponse, error coming when response coming.
string url = "test.slutions.com/.../verbatim";
// declare ascii encoding
// ASCIIEncoding encoding = new ASCIIEncoding();
Encoding encoding = new UTF8Encoding();
string strResult = string.Empty;
// sample xml sent to Service & this data is sent in POST
//string SampleXml = "SHAHZEB";
JArray jr= jarray;
//string postData = jr.ToString();
Console.WriteLine(jr);
// convert xmlstring to byte using ascii encoding
byte[] data = encoding.GetBytes(jr.ToString());
//string data = jr.ToString();
// declare httpwebrequet wrt url defined above
string data11="Shahzeb";
HttpWebRequest webrequest = (HttpWebRequest)WebRequest.Create(url);
// set method as post
webrequest.Method = "POST";
// set content type
webrequest.Headers.Add("apikey", "PVR1234");
webrequest.ContentLength = data.Length;
// get stream data out of webrequest object
Stream newStream = webrequest.GetRequestStream();
newStream.Write(data, 0, data.Length);
newStream.Close();
// declare & read response from service
HttpWebResponse webresponse = (HttpWebResponse)webrequest.GetResponse(); //error on this line when getting response, 401 error
//webresponse.Headers.Add("apikey", "1234");
// set utf8 encoding
string s = webresponse.ToString();
Encoding enc = System.Text.Encoding.GetEncoding( "utf-8" );
// read response stream from response object
StreamReader loResponseStream =
new StreamReader(webresponse.GetResponseStream(), enc);
// read string from stream data
strResult = loResponseStream.ReadToEnd();
Console.WriteLine(strResult);
Console.ReadKey();
// close the stream object
loResponseStream.Close();
// close the response object
webresponse.Close();
// below steps remove unwanted data from response string
strResult = strResult.Replace("</string>", "");
strResult = strResult.Substring(strResult.LastIndexOf(strResult));
Console.WriteLine(strResult);
Console.ReadKey();
}