读取QQ相册

aspx代码

 

<% @ Page Language = " C# "  AutoEventWireup = " true "  CodeFile = " GetPhoto.aspx.cs "  Inherits = " GetPhoto "   %>

<! DOCTYPE html PUBLIC  " -//W3C//DTD XHTML 1.0 Transitional//EN "   " http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd " >

< html xmlns = " http://www.w3.org/1999/xhtml "   >
< head runat = " server " >
    
< title > 读取QQ相册 </ title >
</ head >
< body >
    
< form id = " form1 "  runat = " server "  method = " post " >
    
< div >
    
< P > QQ号码: < asp:textbox id = " txtQQ "  runat = " server " ></ asp:textbox >
                
< asp:button id = " Button1 "  runat = " server "  Text = " 读取相册 "  OnClick = " Button1_Click " ></ asp:button ></ P >
            
< asp:Label ID = " msg "  Runat = " server " ></ asp:Label >
            
< P >
                
< asp:DataList id = " albumList "  Runat = " server "  RepeatColumns = " 8 "  EnableViewState = " False " >
                    
< ItemTemplate >
                        
< div >
                            
< div ><% # DataBinder.Eval(Container.DataItem, " PicLink " ) %></ div >
                            
< div ><% # DataBinder.Eval(Container.DataItem, " PicTitle " ) %></ div >
                        
</ div >
                    
</ ItemTemplate >
                
</ asp:DataList >
                
< asp:DataList id = " picList "  Runat = " server "  RepeatColumns = " 8 "  EnableViewState = " False "  DataKeyField = " PicUrl "  OnUpdateCommand = " picList_UpdateCommand " >
                    
< ItemTemplate >
                        
< div >
                            
< div ><% # DataBinder.Eval(Container.DataItem, " PicLink " ) %></ div >
                            
< div >
                                
< asp:CheckBox ID = " confirm "  Runat = " server "  Text = ' <%# DataBinder.Eval(Container.DataItem,"PicTitle")%> '   /></ div >
                        
</ div >
                    
</ ItemTemplate >
                    
< FooterTemplate >
                        
< div >
                            
< asp:Button ID = " bntOk "  Runat = " server "  CommandName = " Update "  Text = " 开始导入 " ></ asp:Button ></ div >
                    
</ FooterTemplate >
                
</ asp:DataList >
            
</ P >

    
</ div >
    
</ form >
</ body >
</ html >

 

.cs代码

 

using  System;
using  System.Data;
using  System.Configuration;
using  System.Collections;
using  System.Web;
using  System.Web.Security;
using  System.Web.UI;
using  System.Web.UI.WebControls;
using  System.Web.UI.WebControls.WebParts;
using  System.Web.UI.HtmlControls;
using  System.Net;
using  System.IO;
using  System.Xml;
using  System.Drawing.Imaging;


public   partial   class  GetPhoto : System.Web.UI.Page
{
    
private const string QQPHOTO = "http://p{0}.photo.qq.com/{1}/16"//{0} = {1} % 13 + 1 {1}为qq号
    
//取相册的另一个有用路径,在第一个可用的情况下用。
    private const string QQPHOTO_B = "http://photo.qq.com/cgi-bin/common/cgi_list_album?uin={0}";
    
private const string ALBUMURL = "http://sz.photo.store.qq.com/http_staload.cgi?{0}/{1}"//{0}qq号 {1}album号

    
private string UserID = "maxwell"//用户名


    
protected void Page_Load(object sender, EventArgs e)
    
{
        
//点击“开始导入”时 把打勾的图片保存到本机上。
        picList.UpdateCommand += new DataListCommandEventHandler(picList_UpdateCommand);

        
//取url参数中的相册图片 即取qq相册   QQ有防盗链
        if (Request.QueryString["pre"!= null)
        
{
            
string url = Request.QueryString["pre"];

            WebRequest preR 
= WebRequest.Create(url);
            Stream stream 
= preR.GetResponse().GetResponseStream();
            
//QQ相册只能上传jpg|gif|png的图片
            
//把这些图片保存为jpeg格式和ContentType设置为image/jpeg都能显示。
            Response.ClearContent();
            Response.ContentType 
= "image/jpeg";

            System.Drawing.Bitmap img 
= new System.Drawing.Bitmap(stream);
            img.Save(Response.OutputStream, ImageFormat.Jpeg);

            img.Dispose();
            stream.Close();

            Response.End();
        }

        
//根据相册ID取该相册中的图片。
        if (Request.QueryString["id"!= null && Request.QueryString["qq"!= null)
        
{
            
string id = Request.QueryString["id"];
            
string qq = Request.QueryString["qq"];
            
string albumUrl = string.Format(ALBUMURL, qq, id);

            XmlDocument xml 
= GetXmlData(albumUrl);
            
if (xml == null)
            
{
                msg.Text 
= "暂时不能读取数据。请稍后再试。";
                
return;
            }

            XmlNodeList list 
= xml.GetElementsByTagName("pic");

            PicAlbum[] pics 
= new PicAlbum[list.Count];

            
for (int i = 0; i < list.Count; i++)
            
{
                
string name = list[i].SelectSingleNode("name").InnerText;
                
string pre = list[i].SelectSingleNode("pre").InnerText;
                
string url = list[i].SelectSingleNode("url").InnerText;

                
string picLink = "<img src='" + Request.Url.AbsolutePath + "?pre=" + pre + "'/>";
                pics[i] 
= new PicAlbum(url, picLink, name);
            }

            albumList.Visible 
= false;
            picList.Visible 
= true;
            picList.DataSource 
= pics;
            picList.DataBind();
        }

    }


    
//读取相册
    protected void Button1_Click(object sender, EventArgs e)
    
{
        
string qq = txtQQ.Text;
        
string photoUrl = string.Format(QQPHOTO, (int.Parse(qq) % 13 + 1), qq);

        
//从QQ读取相册数据
        XmlDocument xml = GetXmlData(photoUrl);
        
if (xml == null)
        
{
            
//判断静态XML是否取到,如果未取到,再尝试拉一次CGI(QQ的说法)
            string photoBackupUrl = string.Format(QQPHOTO_B, qq);
            xml 
= GetXmlData(photoBackupUrl);
            
if (xml == null)
            
{
                msg.Text 
= "暂时不能读取数据。请稍后再试。";
                
return;
            }

        }

        XmlNodeList list 
= xml.GetElementsByTagName("album");

        ArrayList albums 
= new ArrayList();
        
for (int i = 1; i < list.Count; i++)//从1开始,是因为xml文件的第一个album不是相册数据。
        {
            
string priv = list[i].SelectSingleNode("priv").InnerText.Trim();
            
if (priv != "1")
                
continue;
            
string id = list[i].SelectSingleNode("id").InnerText;
            
string name = list[i].SelectSingleNode("name").InnerText;
            
string pre = list[i].SelectSingleNode("pre").InnerText;

            
string picLink = "<a href='" + Request.Url.AbsolutePath + "?id=" + id + "&qq=" + qq + "'><img src='" + Request.Url.AbsolutePath + "?pre=" + pre + "'/></a>";
            albums.Add(
new PicAlbum(id, picLink, name));

        }

        picList.Visible 
= false;
        albumList.Visible 
= true;
        albumList.DataSource 
= albums;
        albumList.DataBind();


    }


     
/**//// <summary>
        
/// 根据地址得到一个xml文档
        
/// </summary>
        
/// <param name="url">地址</param>
        
/// <returns></returns>

        private XmlDocument GetXmlData(string url)
        
{
            HttpWebRequest wreq 
= (HttpWebRequest)WebRequest.Create(url);
            
//wreq.Timeout = 20;
            HttpWebResponse wres = null;
            XmlDocument xml 
= null;
            
try
            
{
                wres 
= (HttpWebResponse)wreq.GetResponse();
                xml 
= new XmlDocument();
                xml.Load(wres.GetResponseStream());
            }

            
finally
            
{
                wres.Close();
            }

            
return xml;
        }


    
protected void picList_UpdateCommand(object source, DataListCommandEventArgs e)
    
{
         DataList picList 
= source as DataList;
            PicAlbum[] pics 
= (PicAlbum[])picList.DataSource;
            ArrayList urls 
= new ArrayList(); //用来保存要导入图片的地址。
            for(int i=0; i<picList.Items.Count; i++)
            
{
                CheckBox cb 
= (CheckBox)picList.Items[i].FindControl("confirm");
                
if(cb.Checked)
                
{
                    urls.Add(picList.DataKeys[i]);
                }

            }

            
//读取要保存图片的URL后,保存图片
            SavePics(urls);

    }


     
/**//// <summary>
        
/// 保存列表中的图片。
        
/// </summary>
        
/// <param name="urls">保存图片地址的列表</param>

        private void SavePics(ArrayList urls)
        
{
            DateTime date 
= DateTime.Now;
            
string path = Server.MapPath(".");
            
int i=1;
            
foreach(string url in urls)
            
{
                
string name = path + "/" + UserID + date.ToString("yyyyMMdd"+ date.Hour.ToString() 
                    
+ date.Minute.ToString() + date.Second.ToString() 
                    
+ (i++)
                    
+ ".jpg";
                HttpWebRequest req 
= (HttpWebRequest)WebRequest.Create(url);
                Stream stream 
= req.GetResponse().GetResponseStream();

                System.Drawing.Bitmap img 
= new System.Drawing.Bitmap(stream);        
                img.Save(name,ImageFormat.Jpeg);

                img.Dispose();
                stream.Close();
            }

            msg.Text 
= "文件保存完成。";
        }


}


/**/ /// <summary>
    
/// 图片相册类
    
/// </summary>

     public   class  PicAlbum
    
{
        
private string picUrl;
        
private string picLink;
        
private string picTitle;

        
public PicAlbum(string picurl,string piclink,string pictitle)
       
{
            picUrl 
= picurl;
            picLink 
= piclink;
            picTitle 
= pictitle;
        }


        
public string PicUrl
        
{
            
getreturn picUrl;}
        }

        
public string PicLink
        
{
            
getreturn picLink;}
        }

        
public string PicTitle
        
{
            
getreturn picTitle;}
        }

    }




 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值