private void LoginTest()
{
string resultState = string.Empty;
try
{
var request = new Request();
string jdata = "imei=1"+
"&contactid=1001"+
"&password=e10adc3949ba59abbe56e057f20f883e";
var response = request.Execute("http://dumXXXXXX.com.cn/dum/app_login.action", jdata, "POST");
JObject jo = (JObject)JsonConvert.DeserializeObject(response.ToString());
resultState = jo["result"].ToString();
}
catch (Exception ex)
{
errorList.Add(ex.Message);
}
finally
{
switch (resultState)
{
case "200":
resultStateDic["200(成功)"]++;
break;
case "401":
resultStateDic["401(失败)"]++;
break;
case "402":
resultStateDic["402(IMEI不匹配)"]++;
break;
case "403":
resultStateDic["403(没有权限)"]++;
break;
case "404":
resultStateDic["404(用户不存在)"]++;
break;
case "601":
resultStateDic["601(用户存在,但没有登记)"]++;
break;
default:
resultStateDic["other"]++;
break;
}
}
}
/// <summary>
/// Create Http Request, using json, and read Http Response.
/// </summary>
public class Request
{
/// <summary>
/// Url of http server wich request will be created to.
/// </summary>
public String URL { get; set; }
/// <summary>
/// HTTP Verb wich will be used. Eg. GET, POST, PUT, DELETE.
/// </summary>
public String Verb { get; set; }
/// <summary>
/// Request content, Json by default.
/// </summary>
public String Content
{
//get { return "text/json"; }
get { return "application/x-www-form-urlencoded"; }
}
/// <summary>
/// User and Password for Basic Authentication
/// </summary>
public Credentials Credentials { get; set; }
public HttpWebRequest HttpRequest { get; internal set; }
public HttpWebResponse HttpResponse { get; internal set; }
public CookieContainer CookieContainer = new CookieContainer();
public object Execute(string url, object obj, string verb)
{
if (url != null)
URL = url;
if (verb != null)
Verb = verb;
HttpRequest = CreateRequest();
WriteStream(obj);
try
{
HttpResponse = (HttpWebResponse)HttpRequest.GetResponse();
}
catch (WebException error)
{
HttpResponse = (HttpWebResponse)error.Response;
return ReadResponseFromError(error);
}
return ReadResponse();
}
internal HttpWebRequest CreateRequest()
{
var basicRequest = (HttpWebRequest)WebRequest.Create(URL);
basicRequest.ContentType = Content;
basicRequest.Method = Verb;
basicRequest.CookieContainer = CookieContainer;
if (Credentials != null)
basicRequest.Headers.Add("Authorization", "Basic" + " " + EncodeCredentials(Credentials));
return basicRequest;
}
internal void WriteStream(object obj)
{
if (obj != null)
{
using (var streamWriter = new StreamWriter(HttpRequest.GetRequestStream()))
{
if (obj is string)
streamWriter.Write(obj);
else
streamWriter.Write(JsonConvert.SerializeObject(obj));
}
}
}
internal String ReadResponse()
{
if (HttpResponse != null)
using (var streamReader = new StreamReader(HttpResponse.GetResponseStream()))
return streamReader.ReadToEnd();
return string.Empty;
}
internal String ReadResponseFromError(WebException error)
{
using (var streamReader = new StreamReader(error.Response.GetResponseStream()))
return streamReader.ReadToEnd();
}
internal static string EncodeCredentials(Credentials credentials)
{
var strCredentials = string.Format("{0}:{1}", credentials.UserName, credentials.Password);
var encodedCredentials = Convert.ToBase64String(Encoding.UTF8.GetBytes(strCredentials));
return encodedCredentials;
}
}
/// <summary>
/// This class support JsonRequest on sending credentials for Basic Authentication on HTTP server
/// </summary>
public class Credentials
{
public string UserName { get; set; }
public string Password { get; set; }
}