• 欢迎访问开心洋葱网站,在线教程,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站,欢迎加入开心洋葱 QQ群
  • 为方便开心洋葱网用户,开心洋葱官网已经开启复制功能!
  • 欢迎访问开心洋葱网站,手机也能访问哦~欢迎加入开心洋葱多维思维学习平台 QQ群
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏开心洋葱吧~~~~~~~~~~~~~!
  • 由于近期流量激增,小站的ECS没能经的起亲们的访问,本站依然没有盈利,如果各位看如果觉着文字不错,还请看官给小站打个赏~~~~~~~~~~~~~!

C#模拟登陆OSC并获取Cookie

OC/C/C++ 水墨上仙 2345次浏览

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);
		}
		
		
	}
}


开心洋葱 , 版权所有丨如未注明 , 均为原创丨未经授权请勿修改 , 转载请注明C#模拟登陆OSC并获取Cookie
喜欢 (0)
加载中……