开源中国动弹客户端实践(三)

我们只知道通讯的请求和结果,逻辑结构并不是完全明白。
在这里我想将每条动弹的信息原封不动的显示在界面中。
所以需要控件做一些扩展,来将HTML标记转换为视图。
WPF可以很完美的实现自定义控件,开发者的Idea可以很简洁的被呈现,这是我选择WPF的原因。
接下来将构筑控件,这里利用到了WebBrowser,HtmlAgilityPack

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Net;
using System.IO;


namespace OCMove
{
    public enum HttpMethodType
    {
        [Description("GET")]
        GET = 0,
        [Description("POST")]
        POST = 1,
        [Description("PUT")]
        PUT = 2,
    }


    public class HttpObject
    {
        public virtual string Send(HttpMethodType method, 
            string hostUrl, 
            string queryString, 
            WebHeaderCollection header, 
            byte[] requestBody, 
            IWebProxy proxy, 
            CookieContainer cc,
            int timeout, 
            AsyncCallback callback)
        {
            string methodType = "GET";
            string url = hostUrl;
            string referer = "";
            Uri uriRequest = null;
            bool bkeeplive = true;
            string useragent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022)";
            string contenttype = "application/x-www-form-urlencoded";


            #region Method
            switch (method)
            {
                case HttpMethodType.POST:
                    {
                        methodType = "POST";
                        break;
                    }
                case HttpMethodType.PUT:
                    {
                        methodType = "PUT";
                        break;
                    }
                case HttpMethodType.GET:
                default:
                    {
                        methodType = "GET";
                        break;
                    }
            }
            #endregion //Method


            #region URL
            if (!string.IsNullOrEmpty(queryString))
            {
                url = string.Format("{0}?{1}", hostUrl, queryString);
            }
            uriRequest = new Uri(url);






            if (uriRequest.ToString().ToLower().StartsWith("https"))
            {
                ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(CheckValidationResult);
            }
            System.Net.ServicePointManager.Expect100Continue = false;


            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uriRequest);
            #endregion //URL


            #region Headers
            if (header != null)
            {
                
                referer = header[HttpRequestHeader.Referer] as string;
                bkeeplive = Convert.ToBoolean(header[HttpRequestHeader.KeepAlive]);
                
                useragent = header[HttpRequestHeader.UserAgent] as string;
                contenttype = header[HttpRequestHeader.ContentType] as string;
                //request.Headers = header;
            }
            #endregion //Headers


            #region Settings


            if (!string.IsNullOrEmpty(referer))
            {
                request.Referer = referer;
            }
            request.Proxy = proxy == null ? HttpWebRequest.DefaultWebProxy : proxy;
            request.CookieContainer = cc;
            request.KeepAlive = bkeeplive==null ? true : bkeeplive;
            request.UserAgent = string.IsNullOrEmpty(useragent) ? "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022)" : useragent;
            request.Timeout = timeout;
            request.Method = methodType;
            #endregion //Settings
            if (method == HttpMethodType.POST ||
                method == HttpMethodType.PUT)
            {
                request.ContentType = string.IsNullOrEmpty(contenttype) ? "application/x-www-form-urlencoded" : contenttype;
                request.ContentLength = requestBody.Length;


                Stream newStream = request.GetRequestStream();
                newStream.Write(requestBody, 0, requestBody.Length);
                newStream.Close();
            }
            try
            {
                if (callback == null)
                {
                    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                    Encoding encoding = !string.IsNullOrEmpty(response.ContentEncoding) ? Encoding.GetEncoding(response.ContentEncoding) : Encoding.UTF8;
                    Stream stream = response.GetResponseStream();
                    using (StreamReader reader = new StreamReader(stream, encoding))
                    {
                      return  reader.ReadToEnd();
                    }


                   
                }
                else
                {//使用异步方式
                    request.BeginGetResponse(callback, request);
                }
                return HttpStatusCode.OK.ToString();
            }
            catch (Exception ex)
            {
                return ex.Message;
            }


        }
        //https
        private static bool CheckValidationResult(object sender,
            System.Security.Cryptography.X509Certificates.X509Certificate certificate,
            System.Security.Cryptography.X509Certificates.X509Chain chain,
            System.Net.Security.SslPolicyErrors errors)
        { // Always accept
            return true;
        }
    }


    public class HttpAction : HttpObject
    {
        #region Constructors
        public HttpAction()
        { 
        }
        public HttpAction(HttpMethodType mode,string url)
            : this(mode, url, null, null)
        {
        }
        public HttpAction(HttpMethodType mode, string url,string bodydata)
            : this(mode, url, null, bodydata)
        {
        }


        public HttpAction(HttpMethodType mode, string url, WebHeaderCollection headers)
            :this(mode,url,headers,null)
        {
        }
        public HttpAction(HttpMethodType mode, string url,WebHeaderCollection headers ,string bodydata)
        {
            this.ActionMode = mode;
            this.Url = url;
            if (headers != null)
            {
                this.RequestHeaders = headers;
            }
            if (!string.IsNullOrEmpty(bodydata))
            {
                this.BodyData = bodydata;
            }
            this.Encoding = Encoding.UTF8;
            RequestHeaders = new WebHeaderCollection();
        }
        #endregion 


        private CookieContainer m_Cookies = new CookieContainer();
        private int m_TimeOut = 3000;
        private string m_ContentType = "application/x-www-form-urlencoded";
        private WebHeaderCollection m_ResponseHeaders = new WebHeaderCollection();


        public HttpMethodType ActionMode{get;set;}
        public string Url { get; set; }
        public string QueryText { get; set; }
        public WebHeaderCollection RequestHeaders { get; set; }
        public string BodyData { get; set; }
        public Encoding Encoding { get; set; }
        /// <summary>
        /// 设置连接超时时间(ms)
        /// </summary>
        public int TimeOut
        {
            set { m_TimeOut = value; }
            get { return m_TimeOut; }
        }
        /// <summary>
        /// 设置/获取CookieContainer
        /// </summary>
        public CookieContainer Cookies
        {
            get { return m_Cookies; }
            set { m_Cookies = value; }
        }
        
        /// <summary>
        /// 设置请求的内容类型
        /// </summary>
        public string ContentType
        {
            set { m_ContentType = value; }
            get {return m_ContentType;}
        }
        
        /// <summary>
        /// 获取或者设置请求返回的HTTP标头
        /// </summary>
        public WebHeaderCollection ResponseHeaders
        {
            get { return m_ResponseHeaders; }
        }




        /// <summary>
        /// 回调事件(声明了回调函数则使用异步请求)
        /// </summary>
        public event EventHandler<ResponseComplatedArgs> ResponseComplate;


        private void RespCallback(IAsyncResult result)
        {
             string responseText  ="";
            HttpStatusCode code = HttpStatusCode.Unused;
             try
             {
                 HttpWebRequest request = (HttpWebRequest)result.AsyncState;
                 HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(result);
                 this.m_ResponseHeaders = response.Headers;
                 code = response.StatusCode;
                 Encoding encoding = !string.IsNullOrEmpty(response.ContentEncoding) ? Encoding.GetEncoding(response.ContentEncoding) : Encoding.UTF8;
                 Stream stream = response.GetResponseStream();


                 using (StreamReader reader = new StreamReader(stream, encoding))
                 {
                     responseText = reader.ReadToEnd();
                 }
             }
             catch (Exception ex)
             { responseText = ex.Message; }
            if (this.ResponseComplate != null)
            {
                this.ResponseComplate(this, new ResponseComplatedArgs(responseText, m_ResponseHeaders, code));
            }
        }




        public void Invoke()
        {
            base.Send(this.ActionMode, this.Url, this.QueryText, this.RequestHeaders, this.Encoding.GetBytes(this.BodyData), null, this.Cookies, this.TimeOut, new AsyncCallback(RespCallback));
        }
    }


    public class ResponseComplatedArgs:EventArgs
    {
        private string m_Data ="";
        public string ResponseData{get{return m_Data;}}
        private WebHeaderCollection m_Headers = new WebHeaderCollection();
        public WebHeaderCollection ResponseHeaders { get { return m_Headers; } }
        public HttpStatusCode StatusCode { get; private set; }
        public ResponseComplatedArgs()
        {}
        public ResponseComplatedArgs(string data, WebHeaderCollection headers,HttpStatusCode code)
        {
             m_Data=data;
             m_Headers = headers;
             this.StatusCode = code;
         }




    }
}

 
 
 
 

  
  

对开源中国的通信进行实装(未完成)。

using System;
using System.Text;
using System.Net;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;




namespace OCMove
{
       public class OSChina
    {
        #region Actions
        private HttpAction ACTION_LOGIN = new HttpAction(HttpMethodType.POST, "https://www.oschina.net/action/user/hash_login");
        private HttpAction ACTION_DONGTAN = new HttpAction(HttpMethodType.POST, "http://www.oschina.net/action/tweet/pub");
        private HttpAction ACTION_REFRESH = new HttpAction(HttpMethodType.GET, "http://www.oschina.net/widgets/check-top-log?last=undefined");
        private HttpAction ACTION_ANSWER = new HttpAction(HttpMethodType.POST, "http://my.oschina.net/action/tweet/rpl");
        private HttpAction ACTION_MY_INFO = new HttpAction(HttpMethodType.POST, "http://www.oschina.net/action/api/my_information");
        #endregion//Actions


        #region Constructors
       
        public OSChina()
        {
            Init();
        }
        public OSChina(string email,string pwd)
        {
            this.Email = email;
            this.Pwd = pwd;
            Init();
        }
        private void Init()
        {
            ACTION_LOGIN.ResponseComplate += new EventHandler<ResponseComplatedArgs>(ACTION_LOGIN_ResponseComplate);
            ACTION_MY_INFO.ResponseComplate += new EventHandler<ResponseComplatedArgs>(ACTION_MY_INFO_ResponseComplate);
        }


        #endregion //


        #region Properties
        public bool HasLogined
        {
            get;
            private set;
        }


        public string Email
        {
            get;
            set;
        }
        public string Pwd
        {
            get;private set;
        }
        public int UserCode
        {
            get;
            private set;
        }
        public string UserName
        {
            get;
            private set;
        }
        public Uri HomePage
        {
            get;
            private set;
        }
        public CookieCollection Cookies
        {
            get;
            private set;
        }


        public Cookie OscidCookie
        {
            get;
            private set;
        }


           
        #endregion //Properties


        #region Methods
        #region LoginAction
        public event EventHandler<ResponseComplatedArgs> LoginComplated;
        
        public virtual void Login()
        {
            ACTION_LOGIN.RequestHeaders.Add(HttpRequestHeader.Referer, "https://www.oschina.net/home/login?goto_page=http%3A%2F%2Fwww.oschina.net%2F");
            ACTION_LOGIN.BodyData = string.Format("email={0}&pwd={1}&keep_login=1",this.Email,this.Pwd);
            ACTION_LOGIN.Cookies = new CookieContainer();
            ACTION_LOGIN.Invoke();
        }


        void ACTION_LOGIN_ResponseComplate(object sender, ResponseComplatedArgs e)
        {
           if( e.StatusCode == HttpStatusCode.OK)
           {
               this.HasLogined = true;
               this.Cookies = ACTION_LOGIN.Cookies.GetCookies(new Uri("https://www.oschina.net"));
               foreach (Cookie c in this.Cookies)
               {
                   if ("oscid".Equals(c.Name))
                   {
                       this.OscidCookie = c;
                       break;
                   }
               }


           }
            if (this.LoginComplated != null)
            {
                this.LoginComplated(this,e);
            }
        }
        #endregion //Login


        public virtual void GetMyInformation()
        {
            ACTION_MY_INFO.Cookies = new CookieContainer();
            ACTION_LOGIN.Cookies.Add( this.OscidCookie);
            ACTION_MY_INFO.Invoke();
        }


        void ACTION_MY_INFO_ResponseComplate(object sender, ResponseComplatedArgs e)
        {


        }


        #endregion//Methods
    }
}

 
 
 
 

  
  

现在进行下简单验证,首先是登录。

string email = "******@gmail.com";
            string pwd = "****";
            OSChina osc = new OSChina(email,pwd.ToSHA1());
            osc.LoginComplated += new EventHandler<ResponseComplatedArgs>(osc_LoginComplated);
            osc.Login();

转载于:https://my.oschina.net/jickie/blog/149125

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
开源电子签章客户端 c 是一款基于开源技术开发的电子签章客户端软件。这款软件通过使用c语言进行编码,具有跨平台、高效稳定的特点。 首先,开源电子签章客户端 c 的跨平台特性使得它能够在多个操作系统上运行,无论是Windows、Linux还是MacOS,用户都可以方便地使用这款软件进行电子签章操作。这样一来,无论用户在哪个操作系统下工作,都能够享受到开源电子签章客户端 c 提供的便利。 其次,开源电子签章客户端 c 的高效稳定性使得用户能够快速、稳定地完成签章操作。使用c语言进行编码,它能够充分利用计算机的资源,提高程序的运行效率,使得签章过程更加快速和流畅。同时,开源电子签章客户端 c 经过长时间的开发和测试,确保了软件的稳定性和可靠性,用户可以放心地使用该软件进行电子签章。 此外,开源电子签章客户端 c 还提供了丰富的功能和灵活的配置选项,以满足不同用户的需求。用户可以根据自己的实际情况,自定义签章的样式、位置和大小等参数,使得签章结果更加符合个人需求和业务要求。同时,该客户端还支持多种签章文件格式,如PDF、Word、Excel等,用户可以根据需要选择合适的文件格式进行签章操作。 总之,开源电子签章客户端 c 是一款功能强大、稳定高效的电子签章软件,它具备跨平台的特性、高效稳定的运行以及丰富灵活的配置选项。无论用户是个人还是企业,都可以通过使用开源电子签章客户端 c 来简化和提高签章工作的效率,实现数字化办公的目标。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值