public class Login
{
public string username;
public string password;
public string device_id;
public string app_version;
}
#region Post Webservice using HttpWebRequest without using async and await
Login lb = new Login();
public void SendRequest()
{
lb.username = "12345";
lb.password = "12345";
lb.device_id = "FBB63E574ACF3616D126C2AF9CA568323403FDFA";
lb.app_version = "1.0";
ProcessWebRequest("http://schoolccesoftware.com/PportalApp/index.php/WSlogin");
}
public void ProcessWebRequest(string strUrl)
{
string result = null;
try
{
HttpWebRequest HttpWReq = (HttpWebRequest)WebRequest.Create(strUrl);
HttpWReq.Method = "POST";
HttpWReq.ContentType = "application/x-www-form-urlencoded";
HttpWReq.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback), HttpWReq);
}
catch (Exception ex)
{
MessageBox.Show("Something went wrong ");
}
}
public void GetRequestStreamCallback(IAsyncResult asynchronousResult)
{
try
{
HttpWebRequest HttpWReq = (HttpWebRequest)asynchronousResult.AsyncState;
Stream postStream = HttpWReq.EndGetRequestStream(asynchronousResult);
string postData = string.Empty;
postData = Newtonsoft.Json.JsonConvert.SerializeObject(lb);
byte[] byteArray = System.Text.Encoding.UTF8.GetBytes(postData);
postStream.Write(byteArray, 0, byteArray.Length);
postStream.Close();
HttpWReq.BeginGetResponse(new AsyncCallback(GetResponse), HttpWReq);
}
catch (Exception ex)
{
MessageBox.Show("Something went wrong please contact Edsys");
}
}
public void GetResponse(IAsyncResult callbackResult)
{
try
{
// string s="{\"status\":\"false\",\"message\":\"Access denied ! This device has been register to a parent and cannot be used for other parent\\/student. For more assistance reach out to info@edsys.in\"}";
HttpWebRequest HttpWReq = (HttpWebRequest)callbackResult.AsyncState;
HttpWebResponse response = (HttpWebResponse)HttpWReq.EndGetResponse(callbackResult);
using (StreamReader httpWebStreamReader = new StreamReader(response.GetResponseStream()))
{
string result = httpWebStreamReader.ReadToEnd();
if (result == "{\"success\": false}")
{
}
else
{
Login[] lgobj = Newtonsoft.Json.JsonConvert.DeserializeObject<Login[]>(result);
}
}
}
catch (Exception ex)
{
}
}
#endregion
#region Get and Post WebService using HttpWebRequest using async and await
#region Example PostWebService HttpWebRequest using async and await
public async void GePosttResponse()
{
Login l = new Login();
l.username = "12345";
l.password = "12345";
l.device_id = "FBB63E574ACF3616D126C2AF9CA568323403FDFA";
l.app_version = "1.0";
string str = await GetHttpPostMethodResponse("http://schoolccesoftware.com/PportalApp/index.php/WSlogin", l);
}
public async Task<string> GetHttpPostMethodResponse(string url, Login obj)
{
string result = string.Empty;
try
{
HttpWebRequest HttpWReq = (HttpWebRequest)WebRequest.Create(url);
HttpWReq.Method = "POST";
HttpWReq.ContentType = "application/x-www-form-urlencoded";
string postData = Newtonsoft.Json.JsonConvert.SerializeObject(obj);
byte[] byteArray = System.Text.Encoding.UTF8.GetBytes(postData);
using (var postStream = await HttpWReq.GetRequestStreamAsync())
{
// Write to the request stream.
// ASYNC: writing to the POST stream can be slow
await postStream.WriteAsync(byteArray, 0, byteArray.Length);
}
var response = (HttpWebResponse)await HttpWReq.GetResponseAsync();
if (response != null)
{
var reader = new StreamReader(response.GetResponseStream());
// ASYNC: using StreamReader's async method to read to end, in case
// the stream i slarge.
result = await reader.ReadToEndAsync();
}
return result;
}
catch (Exception ex)
{
throw ex;
// return null;
}
}
#endregion
#region Example GetWebService using HttpWebRequest async and await
public async void GetGetMethodResponse()
{
string str = await GetHttpGetMethodResponse("http://app.konzeptmedia.ch/webservice/redeem/W6ZsbYTKkM");
}
public async Task<string> GetHttpGetMethodResponse(string url)
{
string result = string.Empty;
try
{
HttpWebRequest HttpWReq = (HttpWebRequest)WebRequest.Create(url);
HttpWReq.Method = "GET";
HttpWReq.Headers.Add("X-Device-Token","wewewewewe");
HttpWReq.Headers.Add("X-Platform", "win8");
// HttpWReq.ContentType = "application/x-www-form-urlencoded";
HttpWebResponse response = (HttpWebResponse)await HttpWReq.GetResponseAsync();
if (response != null)
{
var reader = new StreamReader(response.GetResponseStream());
// ASYNC: using StreamReader's async method to read to end, in case
// the stream i slarge.
result = await reader.ReadToEndAsync();
}
return result;
}
catch (Exception ex)
{
throw ex;
// return null;
}
}
#endregion
#endregion
#region Get and Post WebService using HttpRequestMessage using async and await
#region GetWebService using HttpRequestMessage
public async void Get_Get_HttpRequestMessage_MethodResponse()
{
string result = await GetHttp_Get_HttpRequestMessage_Method_Response("http://app.konzeptmedia.ch/webservice/redeem/W6ZsbYTKkM");
}
public async static Task<string> GetHttp_Get_HttpRequestMessage_Method_Response(string url)
{
string result = string.Empty;
try
{
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, url);
request.Headers.Add("X-Device-Token", "wewewewewe");
request.Headers.Add("X-Platform", "win8");
HttpClient client = new HttpClient();
HttpResponseMessage response = await client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead);
if (response.StatusCode == HttpStatusCode.OK)
{
result = await response.Content.ReadAsStringAsync();
}
else
{
result = "Error connecting to " + url + " ! Status: " + response.StatusCode;
}
}
catch (Exception ex)
{
result = "Error connecting to " + url + " ! Status: " + ex.Message;
}
return result;
}
#endregion
#region PostWebService using HttpRequestMessage
public async void Get_Post_HttpRequestMessage_MethodResponse()
{
Login l = new Login();
l.username = "12345";
l.password = "12345";
l.device_id = "FBB63E574ACF3616D126C2AF9CA568323403FDFA";
l.app_version = "1.0";
string str = await GetHttp_Post_HttpRequestMessage_Method_Response("http://schoolccesoftware.com/PportalApp/index.php/WSlogin", l);
}
public async static Task<string> GetHttp_Post_HttpRequestMessage_Method_Response(string url, Login obj)
{
string result = string.Empty;
try
{
string postData = Newtonsoft.Json.JsonConvert.SerializeObject(obj);
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, url);
HttpClient client = new HttpClient();
request.Content = new StringContent(postData, Encoding.UTF8, "application/x-www-form-urlencoded");
HttpResponseMessage response = await client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead);
result = await response.Content.ReadAsStringAsync();
//await client.SendAsync(request).ContinueWith(respTask =>
// {
// // Console.WriteLine("Response: {0}", respTask.Result);
// result = respTask.Result.ToString();
// });
return result;
}
catch (Exception ex)
{
return null;
}
}
#endregion
#endregion
{
public string username;
public string password;
public string device_id;
public string app_version;
}
#region Post Webservice using HttpWebRequest without using async and await
Login lb = new Login();
public void SendRequest()
{
lb.username = "12345";
lb.password = "12345";
lb.device_id = "FBB63E574ACF3616D126C2AF9CA568323403FDFA";
lb.app_version = "1.0";
ProcessWebRequest("http://schoolccesoftware.com/PportalApp/index.php/WSlogin");
}
public void ProcessWebRequest(string strUrl)
{
string result = null;
try
{
HttpWebRequest HttpWReq = (HttpWebRequest)WebRequest.Create(strUrl);
HttpWReq.Method = "POST";
HttpWReq.ContentType = "application/x-www-form-urlencoded";
HttpWReq.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback), HttpWReq);
}
catch (Exception ex)
{
MessageBox.Show("Something went wrong ");
}
}
public void GetRequestStreamCallback(IAsyncResult asynchronousResult)
{
try
{
HttpWebRequest HttpWReq = (HttpWebRequest)asynchronousResult.AsyncState;
Stream postStream = HttpWReq.EndGetRequestStream(asynchronousResult);
string postData = string.Empty;
postData = Newtonsoft.Json.JsonConvert.SerializeObject(lb);
byte[] byteArray = System.Text.Encoding.UTF8.GetBytes(postData);
postStream.Write(byteArray, 0, byteArray.Length);
postStream.Close();
HttpWReq.BeginGetResponse(new AsyncCallback(GetResponse), HttpWReq);
}
catch (Exception ex)
{
MessageBox.Show("Something went wrong please contact Edsys");
}
}
public void GetResponse(IAsyncResult callbackResult)
{
try
{
// string s="{\"status\":\"false\",\"message\":\"Access denied ! This device has been register to a parent and cannot be used for other parent\\/student. For more assistance reach out to info@edsys.in\"}";
HttpWebRequest HttpWReq = (HttpWebRequest)callbackResult.AsyncState;
HttpWebResponse response = (HttpWebResponse)HttpWReq.EndGetResponse(callbackResult);
using (StreamReader httpWebStreamReader = new StreamReader(response.GetResponseStream()))
{
string result = httpWebStreamReader.ReadToEnd();
if (result == "{\"success\": false}")
{
}
else
{
Login[] lgobj = Newtonsoft.Json.JsonConvert.DeserializeObject<Login[]>(result);
}
}
}
catch (Exception ex)
{
}
}
#endregion
#region Get and Post WebService using HttpWebRequest using async and await
#region Example PostWebService HttpWebRequest using async and await
public async void GePosttResponse()
{
Login l = new Login();
l.username = "12345";
l.password = "12345";
l.device_id = "FBB63E574ACF3616D126C2AF9CA568323403FDFA";
l.app_version = "1.0";
string str = await GetHttpPostMethodResponse("http://schoolccesoftware.com/PportalApp/index.php/WSlogin", l);
}
public async Task<string> GetHttpPostMethodResponse(string url, Login obj)
{
string result = string.Empty;
try
{
HttpWebRequest HttpWReq = (HttpWebRequest)WebRequest.Create(url);
HttpWReq.Method = "POST";
HttpWReq.ContentType = "application/x-www-form-urlencoded";
string postData = Newtonsoft.Json.JsonConvert.SerializeObject(obj);
byte[] byteArray = System.Text.Encoding.UTF8.GetBytes(postData);
using (var postStream = await HttpWReq.GetRequestStreamAsync())
{
// Write to the request stream.
// ASYNC: writing to the POST stream can be slow
await postStream.WriteAsync(byteArray, 0, byteArray.Length);
}
var response = (HttpWebResponse)await HttpWReq.GetResponseAsync();
if (response != null)
{
var reader = new StreamReader(response.GetResponseStream());
// ASYNC: using StreamReader's async method to read to end, in case
// the stream i slarge.
result = await reader.ReadToEndAsync();
}
return result;
}
catch (Exception ex)
{
throw ex;
// return null;
}
}
#endregion
#region Example GetWebService using HttpWebRequest async and await
public async void GetGetMethodResponse()
{
string str = await GetHttpGetMethodResponse("http://app.konzeptmedia.ch/webservice/redeem/W6ZsbYTKkM");
}
public async Task<string> GetHttpGetMethodResponse(string url)
{
string result = string.Empty;
try
{
HttpWebRequest HttpWReq = (HttpWebRequest)WebRequest.Create(url);
HttpWReq.Method = "GET";
HttpWReq.Headers.Add("X-Device-Token","wewewewewe");
HttpWReq.Headers.Add("X-Platform", "win8");
// HttpWReq.ContentType = "application/x-www-form-urlencoded";
HttpWebResponse response = (HttpWebResponse)await HttpWReq.GetResponseAsync();
if (response != null)
{
var reader = new StreamReader(response.GetResponseStream());
// ASYNC: using StreamReader's async method to read to end, in case
// the stream i slarge.
result = await reader.ReadToEndAsync();
}
return result;
}
catch (Exception ex)
{
throw ex;
// return null;
}
}
#endregion
#endregion
#region Get and Post WebService using HttpRequestMessage using async and await
#region GetWebService using HttpRequestMessage
public async void Get_Get_HttpRequestMessage_MethodResponse()
{
string result = await GetHttp_Get_HttpRequestMessage_Method_Response("http://app.konzeptmedia.ch/webservice/redeem/W6ZsbYTKkM");
}
public async static Task<string> GetHttp_Get_HttpRequestMessage_Method_Response(string url)
{
string result = string.Empty;
try
{
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, url);
request.Headers.Add("X-Device-Token", "wewewewewe");
request.Headers.Add("X-Platform", "win8");
HttpClient client = new HttpClient();
HttpResponseMessage response = await client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead);
if (response.StatusCode == HttpStatusCode.OK)
{
result = await response.Content.ReadAsStringAsync();
}
else
{
result = "Error connecting to " + url + " ! Status: " + response.StatusCode;
}
}
catch (Exception ex)
{
result = "Error connecting to " + url + " ! Status: " + ex.Message;
}
return result;
}
#endregion
#region PostWebService using HttpRequestMessage
public async void Get_Post_HttpRequestMessage_MethodResponse()
{
Login l = new Login();
l.username = "12345";
l.password = "12345";
l.device_id = "FBB63E574ACF3616D126C2AF9CA568323403FDFA";
l.app_version = "1.0";
string str = await GetHttp_Post_HttpRequestMessage_Method_Response("http://schoolccesoftware.com/PportalApp/index.php/WSlogin", l);
}
public async static Task<string> GetHttp_Post_HttpRequestMessage_Method_Response(string url, Login obj)
{
string result = string.Empty;
try
{
string postData = Newtonsoft.Json.JsonConvert.SerializeObject(obj);
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, url);
HttpClient client = new HttpClient();
request.Content = new StringContent(postData, Encoding.UTF8, "application/x-www-form-urlencoded");
HttpResponseMessage response = await client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead);
result = await response.Content.ReadAsStringAsync();
//await client.SendAsync(request).ContinueWith(respTask =>
// {
// // Console.WriteLine("Response: {0}", respTask.Result);
// result = respTask.Result.ToString();
// });
return result;
}
catch (Exception ex)
{
return null;
}
}
#endregion
#endregion