using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Security.Cryptography;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Newtonsoft.Json;
using System.Web.Script.Serialization;
public partial class Default3 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
/* string username = TextBox1.Text.Trim();
string password = TextBox2.Text.Trim();
string[] array = {"username=" +username,"password="+password };
List<string> list = array.ToList();
var jsonSerialiser = new JavaScriptSerializer();
string jsonvalues = jsonSerialiser.Serialize(list);*/
var obj = new Newtonsoft.Json.Linq.JObject();
obj["username"] = TextBox1.Text;
obj["password"] = TextBox2.Text;
var serialized = JsonConvert.SerializeObject(obj);
// Response.Write(serialized);
//create the constructor with post type and few data
MyWebRequest myRequest = new MyWebRequest("url u want to access", "POST",serialized);
//show the response string on the console screen.
var deserl = myRequest.GetResponse();
// Response.Write(deserl);
Newtonsoft.Json.Linq.JObject jsonDes = Newtonsoft.Json.Linq.JObject.Parse(deserl);
// Response.Write(jsonDes);
string statuscode = jsonDes["status"].ToString();
Session["userid"] = jsonDes["userid"].ToString();
//Response.Write(statuscode);
if(statuscode=="Success")
{
string uid=jsonDes["userid"].ToString();
//Response.Write(uid);
// Session["username"] = jsonDes["username"].ToString();
Session["userid"] = jsonDes["userid"].ToString();
Response.Redirect("~/Default4.aspx");
}
else
{
Label1.Visible = true;
}
}
public class MyWebRequest
{
private WebRequest request;
private Stream dataStream;
private string status;
public String Status
{
get
{
return status;
}
set
{
status = value;
}
}
public MyWebRequest(string url)
{
// Create a request using a URL that can receive a post.
request = WebRequest.Create(url);
}
public MyWebRequest(string url, string method)
: this(url)
{
if (method.Equals("GET") || method.Equals("POST"))
{
// Set the Method property of the request to POST.
request.Method = method;
}
else
{
throw new Exception("Invalid Method Type");
}
}
public MyWebRequest(string url, string method, string data)
: this(url, method)
{
// Create POST data and convert it to a byte array.
string postData = data;
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
// Set the ContentType property of the WebRequest.
request.ContentType = "application/json";
// Set the ContentLength property of the WebRequest.
request.ContentLength = byteArray.Length;
// Get the request stream.
dataStream = request.GetRequestStream();
// Write the data to the request stream.
dataStream.Write(byteArray, 0, byteArray.Length);
// Close the Stream object.
dataStream.Close();
}
public string GetResponse()
{
// Get the original response.
WebResponse response = request.GetResponse();
this.Status = ((HttpWebResponse)response).StatusDescription;
// Get the stream containing all content returned by the requested server.
dataStream = response.GetResponseStream();
// Open the stream using a StreamReader for easy access.
StreamReader reader = new StreamReader(dataStream);
// Read the content fully up to the end.
string responseFromServer = reader.ReadToEnd();
// Clean up the streams.
reader.Close();
dataStream.Close();
response.Close();
// Console.Write(responseFromServer);
return responseFromServer;
}
}
}