[C#] C#模拟登陆OSC并获取Cookie →→→→→进入此内容的聊天室

来自 , 2019-03-08, 写在 C#, 查看 127 次.
URL http://www.code666.cn/view/46489c17
  1. /*
  2.  * 由SharpDevelop创建。
  3.  * 用户: dodola
  4.  * 日期: 2012/10/23
  5.  * 时间: 9:43
  6.  *
  7.  * 要改变这种模板请点击 工具|选项|代码编写|编辑标准头文件
  8.  */
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Collections.Specialized;
  12. using System.IO;
  13. using System.Net;
  14. using System.Text;
  15.  
  16. namespace OSCLogin
  17. {
  18.         public class PostData
  19.         {
  20.  
  21.                 private List<PostDataParam> m_Params;
  22.  
  23.                 public List<PostDataParam> Params
  24.                 {
  25.                         get { return m_Params; }
  26.                         set { m_Params = value; }
  27.                 }
  28.  
  29.                 public PostData()
  30.                 {
  31.                         m_Params = new List<PostDataParam>();
  32.  
  33.  
  34.                 }
  35.  
  36.                
  37.                 public string GetPostData(string id)
  38.                 {
  39.                        
  40.                         string formDataBoundary = String.Format("--{0:N}",id );
  41.  
  42.                         StringBuilder sb = new StringBuilder();
  43.                        
  44.                         foreach (PostDataParam p in m_Params)
  45.                         {
  46.                                 sb.AppendLine(formDataBoundary);
  47.  
  48.                                 if (p.Type == PostDataParamType.File)
  49.                                 {
  50.                                         sb.AppendLine(string.Format("Content-Disposition: file; name=\"{0}\"; filename=\"{1}\"", p.Name, p.FileName));
  51.                                         sb.AppendLine("Content-Type: text/plain; charset=UTF-8");
  52.                                         sb.AppendLine("Content-Transfer-Encoding: 8bit");
  53.                                         sb.AppendLine();
  54.                                         sb.AppendLine(p.Value);
  55.                                 }
  56.                                 else
  57.                                 {
  58.                                         sb.AppendLine(string.Format("Content-Disposition: form-data; name=\"{0}\"", p.Name));
  59.                                         sb.AppendLine("Content-Type: text/plain; charset=UTF-8");
  60.                                         sb.AppendLine("Content-Transfer-Encoding: 8bit");
  61.                                         sb.AppendLine();
  62.                                         sb.AppendLine(p.Value);
  63.                                 }
  64.                         }
  65.  
  66.                         sb.Append(formDataBoundary+"--");
  67.  
  68.                         return sb.ToString();
  69.                 }
  70.         }
  71.  
  72.         public enum PostDataParamType
  73.         {
  74.                 Field,
  75.                 File
  76.         }
  77.  
  78.         public class PostDataParam
  79.         {
  80.  
  81.  
  82.                 public PostDataParam(string name, string value, PostDataParamType type)
  83.                 {
  84.                         Name = name;
  85.                         Value = value;
  86.                         Type = type;
  87.                 }
  88.  
  89.                 public string Name;
  90.                 public string FileName;
  91.                 public string Value;
  92.                 public PostDataParamType Type;
  93.         }
  94.         class Program
  95.         {
  96.                
  97.                
  98.                 private static String getUserAgent() {
  99.                         return "OSChina.NET/1.6.2 Beta1_12/Android/4.0.4/MI-ONE Plus/"+Guid.NewGuid();
  100.                 }
  101.                
  102.                 private static void Login(){
  103.                         HttpWebRequest request = (HttpWebRequest)
  104.                                 WebRequest.Create("http://www.oschina.net/action/api/login_validate");
  105.                         request.UserAgent=getUserAgent();
  106.                         request.Method = "POST";
  107.                        
  108.                         PostData pData = new PostData();
  109.  
  110.                         pData.Params.Add(new PostDataParam("username", "username", PostDataParamType.Field));
  111.                         pData.Params.Add(new PostDataParam("pwd", "pwd", PostDataParamType.Field));
  112.                         pData.Params.Add(new PostDataParam("keep_login", "1", PostDataParamType.Field));
  113.  
  114.                         string  id=Guid.NewGuid().ToString();
  115.                         byte[] buffer = UTF8Encoding.UTF8.GetBytes(pData.GetPostData(id));
  116.                         request.ContentLength = buffer.Length;
  117.                         request.ContentType="multipart/form-data; boundary="+id;
  118.                         Stream oStream = request.GetRequestStream();
  119.                         oStream.Write(buffer, 0, buffer.Length);
  120.                         oStream.Close();
  121.                         //request.BeginGetResponse(new AsyncCallback(ReadCallback), request);
  122.                         HttpWebResponse response=(HttpWebResponse)request.GetResponse();
  123.                         System.IO.StreamReader sr = new StreamReader(response.GetResponseStream());
  124.                         Console.Write(sr.ReadToEnd());
  125.                         sr.Close();
  126.                         response.Close();
  127.  
  128.                         string cookie = response.Headers.Get("Set-Cookie");
  129.                         Console.WriteLine(cookie);
  130.  
  131.                 }
  132.                
  133.                 private static void ReadCallback(IAsyncResult asynchronousResult)
  134.                 {
  135.                         HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
  136.                         HttpWebResponse response = (HttpWebResponse)
  137.                                 request.EndGetResponse(asynchronousResult);
  138.  
  139.                         foreach (Cookie cookieValue in response.Cookies)
  140.                         {
  141.                                 Console.WriteLine("Cookie: " + cookieValue.ToString());
  142.                         }
  143.                 }
  144.  
  145.                
  146.                 public static void Main(string[] args)
  147.                 {
  148.                         Console.WriteLine("start  login....hoho");
  149.                        
  150.                         Login();
  151.                         Console.Write("Press any key to continue . . . ");
  152.                         Console.ReadKey(true);
  153.                 }
  154.                
  155.                
  156.         }
  157. }
  158. //csharp/5555

回复 "C#模拟登陆OSC并获取Cookie"

这儿你可以回复上面这条便签

captcha