如下内容段是关于C#模拟登陆OSC并获取Cookie的内容,希望能对小伙伴们有一些用途。
using System; using System.Collections.Generic; using System.Collections.Specialized; using System.IO; using System.Net; using System.Text;
namespace OSCLogin { public class PostData {
private List<PostDataParam> m_Params;
public List<PostDataParam> Params
{
get { return m_Params; }
set { m_Params = value; }
}
public PostData()
{
m_Params = new List<PostDataParam>();
}
public string GetPostData(string id)
{
string formDataBoundary = String.Format("--{0:N}",id );
StringBuilder sb = new StringBuilder();
foreach (PostDataParam p in m_Params)
{
sb.AppendLine(formDataBoundary);
if (p.Type == PostDataParamType.File)
{
sb.AppendLine(string.Format("Content-Disposition: file; name="{0}"; filename="{1}"", p.Name, p.FileName));
sb.AppendLine("Content-Type: text/plain; charset=UTF-8");
sb.AppendLine("Content-Transfer-Encoding: 8bit");
sb.AppendLine();
sb.AppendLine(p.Value);
}
else
{
sb.AppendLine(string.Format("Content-Disposition: form-data; name="{0}"", p.Name));
sb.AppendLine("Content-Type: text/plain; charset=UTF-8");
sb.AppendLine("Content-Transfer-Encoding: 8bit");
sb.AppendLine();
sb.AppendLine(p.Value);
}
}
sb.Append(formDataBoundary+"--");
return sb.ToString();
}
}
public enum PostDataParamType
{
Field,
File
}
public class PostDataParam
{
public PostDataParam(string name, string value, PostDataParamType type)
{
Name = name;
Value = value;
Type = type;
}
public string Name;
public string FileName;
public string Value;
public PostDataParamType Type;
}
class Program
{
private static String getUserAgent() {
return "OSChina.NET/1.6.2 Beta1_12/Android/4.0.4/MI-ONE Plus/"+Guid.NewGuid();
}
private static void Login(){
HttpWebRequest request = (HttpWebRequest)
request.UserAgent=getUserAgent();
request.Method = "POST";
PostData pData = new PostData();
pData.Params.Add(new PostDataParam("username", "username", PostDataParamType.Field));
pData.Params.Add(new PostDataParam("pwd", "pwd", PostDataParamType.Field));
pData.Params.Add(new PostDataParam("keep_login", "1", PostDataParamType.Field));
string id=Guid.NewGuid().ToString();
byte[] buffer = UTF8Encoding.UTF8.GetBytes(pData.GetPostData(id));
request.ContentLength = buffer.Length;
request.ContentType="multipart/form-data; boundary="+id;
Stream oStream = request.GetRequestStream();
oStream.Write(buffer, 0, buffer.Length);
oStream.Close();
HttpWebResponse response=(HttpWebResponse)request.GetResponse();
System.IO.StreamReader sr = new StreamReader(response.GetResponseStream());
Console.Write(sr.ReadToEnd());
sr.Close();
response.Close();
string cookie = response.Headers.Get("Set-Cookie");
Console.WriteLine(cookie);
}
private static void ReadCallback(IAsyncResult asynchronousResult)
{
HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
HttpWebResponse response = (HttpWebResponse)
request.EndGetResponse(asynchronousResult);
foreach (Cookie cookieValue in response.Cookies)
{
Console.WriteLine("Cookie: " + cookieValue.ToString());
}
}
public static void Main(string[] args)
{
Console.WriteLine("start login....hoho");
Login();
Console.Write("Press any key to continue . . . ");
Console.ReadKey(true);
}
}
}