asp.net抓取sina邮箱联系人实现代码

作者:袖梨 2022-06-25
 代码如下 复制代码

using System;
using System.Net;
using System.IO;
using System.Text;
using System.Collections.Specialized;
using System.Text.RegularExpressions;
/*
 * AddressSina
 * 新浪免费邮箱获取联系人(sina.com/sina.cn)
 *
 *  改?履?s
 * ver 1.00.00 @[20100721]  星缘梦雨 新?
 * ver 2.00.00 @[20110807]  星缘梦雨 升级 
 */

namespace GetEmailAddress
{
 internal class AddressSina : AddressBase
 {
  private string refererurl;//存放请求地址
        //发送邮箱请求登录
  public override GetAddressStatus Login()
  {
   try
   {
                //20110807 新规
                //取得以com或者cn结尾的新浪免费邮箱类型。
                string url = "";
                string postdata = "";
                string mailType = this.Credential.UserName.Substring(this.Credential.UserName.IndexOf("@"));
                switch (mailType)
                {
                    case"@sina.com":
                        //20110807修正
                        url = "http://mail.sina.com.cn/cgi-bin/login.cgi";
                        //请求登录获得postdata数据
                        postdata = "logintype=uid&u=" + this.Credential.UserName.Substring(0, this.Credential.UserName.IndexOf("@")) + "&psw=" + this.Credential.Password + "&product=mail&%B5%C7%C2%BC=%B5%C7+%C2%BC";
                         break;
                    case "@sina.cn":
                        //20110807新规
                        url = "http://mail.sina.com.cn/cgi-bin/cnlogin.php";
                        //请求登录获得postdata数据
                        postdata ="domain=sina.cn&logintype=uid&u=" + this.Credential.UserName.Substring(0, this.Credential.UserName.IndexOf("@")) + "&psw=" + this.Credential.Password + "&x=40&y=19";
                        break;
                    default: break;
                }
                //postdata数据长度
                byte[] buf = System.Text.Encoding.UTF8.GetBytes(postdata);
                HttpWebResponse loginresponse = SendRequest("POST", url, " http://m0.mail.sina.com.cn/classic/index.php", buf);
    refererurl = loginresponse.ResponseUri.ToString();
    //ResponseUri地址更换,登录成功
    if(refererurl==url)
     return GetAddressStatus.UidOrPwdError;
   }
   catch
   {
    return GetAddressStatus.NetError;
   }
   return GetAddressStatus.Success;
  }
        ///


        /// 返回邮箱数组。
        ///

        /// 取得数组形式邮件信息。
        /// 编码形式以防乱码。
        /// 返回邮箱数组数据。
  public override bool GetAddressPage(out string page,Encoding encoding)
  {
   page = null;
   //通讯录url
   string host = refererurl.Substring(7);
   host = host.Substring(0,host.IndexOf("/"));
   string addressurl = "http://"+host+"/classic/addr_member.php";
            //act=list&gid=0&sort_item=letter&sort_type=desc
            //这里不加上gid=0是因为加0就把整个页面数据全抓取出来了。去掉只抓取contacts数组里面的数据
   string postdata = "act=list&sort_item=letter&sort_type=desc";
   //请求参数
   //act=list&sort_item=letter&sort_type=desc
   byte[] buf = System.Text.Encoding.UTF8.GetBytes(postdata);
   try
   {
    HttpWebResponse sinaAddressResponse = SendRequest("POST",addressurl,refererurl,buf);
    page = GetPageByResponse(sinaAddressResponse,encoding);
    //注销
    string logouturl = "http://"+host+"/classic/logout.php?from=mail";
    HttpWebResponse sinalogoutResponse = SendRequest("GET",logouturl);
   }
   catch
   {
    return false;
   }
   return true;
  }
 }
}

相关文章

精彩推荐