asp.net读取QQ相册的实现代码
本文导语: 1,aspx代码部分 代码示例: 读取QQ相册_www. QQ号码: ...
1,aspx代码部分
读取QQ相册_www.
QQ号码:
2,.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 = "";
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 = "";
albums.Add(new PicAlbum(id, picLink, name));
}
picList.Visible = false;
albumList.Visible = true;
albumList.DataSource = albums;
albumList.DataBind();
}
/**////
/// 根据地址得到一个xml文档
///
/// 地址
///
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