using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;
namespace WebApplication3
{
public class errormsg
{
public string resultCode { get; set; }
public string message { get; set; }
}
public partial class author : System.Web.UI.Page
{
public string buffer = "";
protected void Page_Load(object sender, EventArgs e)
{
var strUrl = "XXXXX";
IDictionary<string, string> parameters = new Dictionary<string, string>();
parameters.Add("name", code);
parameters.Add("pswd", code);
string strResult = this.iPost(strUrl, parameters, 5000);
try
{
var pp = JsonConvert.DeserializeObject<errormsg>(strResult);
if (pp.resultCode != "0")
{
Response.Write("login fail:"+buffer);
Response.End();
}
}catch(Exception ex){}
}
private string iPost(string strUrl, IDictionary<string, string> parameters, int timeout)
{
try
{
HttpWebRequest request = WebRequest.Create(strUrl) as HttpWebRequest;
request.ServicePoint.Expect100Continue = false;
request.ServicePoint.UseNagleAlgorithm = false;
request.Method = "POST";
request.ContentType = "application/json";
request.Accept = "application/json";
request.Timeout = timeout;
Encoding requestEncoding = Encoding.GetEncoding("UTF-8");
if (!(parameters == null || parameters.Count == 0))
{
buffer=JsonConvert.SerializeObject(parameters);
byte[] data = requestEncoding.GetBytes(buffer);
using (Stream stream = request.GetRequestStream())
{
stream.Write(data, 0, data.Length);
}
}
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
string encoding = response.ContentEncoding;
if (encoding == null || encoding.Length < 1)
{
encoding = "UTF-8";
}
StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding(encoding));
string returnData = reader.ReadToEnd();
reader.Dispose();
response.Close();
return returnData;
}
catch (Exception ex)
{
return "error:<br> send:"+ buffer +";<br>"+ex.ToString();
}
}
}
}