public static string Login()
{
string cookie_ck = string.Empty;
try
{
string url = "http://10.122.66.157/api/user/userLogin";
string sendData = "code=01&login=[\"10.192.111.79\",\"hhs\",\"hhs\"]&sql=[\"lhd\",\"1234567qwe\"]";
System.Net.HttpWebRequest httpRquest = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(url);
httpRquest.Method = "POST";
httpRquest.CookieContainer = new System.Net.CookieContainer();
httpRquest.ContentType = "application/x-www-form-urlencoded;charset=UTF-8";
System.IO.Stream requestStream = null;
if (!string.IsNullOrWhiteSpace(sendData))
{
byte[] dataArray = System.Text.Encoding.UTF8.GetBytes(sendData);
requestStream = httpRquest.GetRequestStream();
requestStream.Write(dataArray, 0, dataArray.Length);
requestStream.Close();
}
System.Net.HttpWebResponse response = (System.Net.HttpWebResponse)httpRquest.GetResponse();
Console.WriteLine("返回的cookie:");
var json = Newtonsoft.Json.JsonConvert.SerializeObject(response.Cookies);
Console.WriteLine(json);
foreach (System.Net.Cookie item in response.Cookies)
{
string cookie = item.Name + "=" + item.Value;
Console.WriteLine(cookie);
if (item.Name.Equals("JSESSIONID"))
{
Console.WriteLine("身份需要的cookie:" + item.Name);
cookie_ck = item.Value;
}
}
System.IO.Stream responseStream = response.GetResponseStream();
System.IO.StreamReader reader = new System.IO.StreamReader(responseStream, System.Text.Encoding.UTF8);
string backMsg = reader.ReadToEnd();
Console.WriteLine(backMsg);
reader.Close();
reader.Dispose();
requestStream?.Dispose();
responseStream.Close();
responseStream.Dispose();
}
catch (Exception ex)
{
Console.WriteLine("异常=" + ex.ToString());
}
return cookie_ck;
}
|