|
[mw_shl_code=csharp,true]public static string PostLogin(string postData, string requestUrlString, ref CookieContainer cookie)
{
ASCIIEncoding encoding = new ASCIIEncoding();
byte[] data = encoding.GetBytes(postData);
//向服务端请求
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(requestUrlString);
myRequest.Method = "POST";
myRequest.ContentType = "application/x-www-form-urlencoded";
myRequest.ContentLength = data.Length;
myRequest.CookieContainer = new CookieContainer();
Stream newStream = myRequest.GetRequestStream();
newStream.Write(data, 0, data.Length);
newStream.Close();
//将请求的结果发送给客户端(界面、应用)
HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
cookie.Add(myResponse.Cookies);
StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
return reader.ReadToEnd();
}[/mw_shl_code]
PostRequest :登录后使用Cookie进行其他操作[mw_shl_code=csharp,true]public static string PostRequest(string postData, string requestUrlString, CookieContainer cookie)
{
ASCIIEncoding encoding = new ASCIIEncoding();
byte[] data = encoding.GetBytes(postData);
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(requestUrlString);
myRequest.Method = "POST";
myRequest.ContentType = "application/x-www-form-urlencoded";
myRequest.ContentLength = data.Length;
myRequest.CookieContainer = cookie;
Stream newStream = myRequest.GetRequestStream();
newStream.Write(data, 0, data.Length);
newStream.Close();
HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
return reader.ReadToEnd();
}[/mw_shl_code]e.g.
[mw_shl_code=csharp,true]string strIMSPhone = tb_IMSPhone.Text.Trim();
string strIMSPwd = tb_IMSPwd.Text.Trim();
string postData = "username=" + strIMSPhone + "&password=" + strIMSPwd + "&type=2";
CookieContainer cookie=new CookieContainer();
if (IMSHelper.PostLogin(postData, post_signIn, ref cookie).Equals("ok"))
{
string strCont = PostRequest("", post_getContJsonData, cookie);
}[/mw_shl_code]
|
|