C#模拟登陆OSC并获取Cookie
/* * 由SharpDevelop创建。 * 用户: dodola * 日期: 2012/10/23 * 时间: 9:43 * * 要改变这种模板请点击 工具|选项|代码编写|编辑标准头文件 */ 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) WebRequest.Create("http://www.oschina.net/action/api/login_validate"); 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(); //request.BeginGetResponse(new AsyncCallback(ReadCallback), request); 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); } } }