0

我是 C# 新手,在将 XML Atom 提要从 Gmail 保存到 xml 文件时遇到了一些困难。我确定我离我需要去的地方还有几英里远,我很尴尬地问这个问题,但我自己一个人什么都没有:(

我正在使用一段时间以来一直存在的 GmailHandler 类。

GmailHandler.cs

using System;
using System.Data;
using System.Xml;
using System.Net;
using System.IO;
/*
 * this code made by Ahmed Essawy
 * AhmedEssawy@gmail.com
 * http://fci-h.blogspot.com
 */
/// <summary>
/// Summary description for Class1
/// </summary>
public class GmailHandler
{
    private string username;
    private string password;
    private string gmailAtomUrl;

    public string GmailAtomUrl
    {
        get { return gmailAtomUrl; }
        set { gmailAtomUrl = value; }
    }

    public string Password
    {
        get { return password; }
        set { password = value; }
    }

    public string Username
    {
        get { return username; }
        set { username = value; }
    }

    public GmailHandler(string _Username, string _Password, string _GmailAtomUrl)
    {
        Username = _Username;
        Password = _Password;
        GmailAtomUrl = _GmailAtomUrl;
    }

    public GmailHandler(string _Username, string _Password)
    {
        Username = _Username;
        Password = _Password;
        GmailAtomUrl = "https://mail.google.com/mail/feed/atom";
    }
    public XmlDocument GetGmailAtom()
    {
        byte[] buffer = new byte[8192];
        int byteCount = 0;
        XmlDocument _feedXml = null;
        try
        {
            System.Text.StringBuilder sBuilder = new System.Text.StringBuilder();
            WebRequest webRequest = WebRequest.Create(GmailAtomUrl);

            webRequest.PreAuthenticate = true;

            System.Net.NetworkCredential credentials = new NetworkCredential(this.Username, this.Password);
            webRequest.Credentials = credentials;

            WebResponse webResponse = webRequest.GetResponse();
            Stream stream = webResponse.GetResponseStream();

            while ((byteCount = stream.Read(buffer, 0, buffer.Length)) > 0)
                sBuilder.Append(System.Text.Encoding.ASCII.GetString(buffer, 0, byteCount));


            _feedXml = new XmlDocument();
            _feedXml.LoadXml(sBuilder.ToString());


        }
        catch (Exception ex)
        {
            //add error handling
            throw ex;
        }
        return _feedXml;
    }

}

然后我在这里得到了我的 Program.cs:

我假设问题出在下面的代码上,因为我对此负责,而不是上面的代码。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace GmailAtom
{
    class Program
    {
        static void Main()
        {



            //Create the object from GmailHandler class
            GmailHandler gmailFeed = new GmailHandler("username", "password");

            //get the feed
            XmlDocument myXml = gmailFeed.GetGmailAtom();


            XmlTextWriter writer = new XmlTextWriter("data.xml", null);
            writer.Formatting = Formatting.Indented;
            myXml.Save(writer);




        }
    }
}

当我运行程序时,我得到“WebException 未处理 - 远程服务器返回错误:(407) 需要代理身份验证。”

在此处输入图像描述

任何意见,将不胜感激!

4

1 回答 1

1

我已经尝试了代码,它工作正常(但我的网络中没有代理)。

我更改了 GmailHandler.cs,构造函数现在接受 Internet 代理。

using System;
using System.Data;
using System.Xml;
using System.Net;
using System.IO;
/*
 * this code made by Ahmed Essawy
 * AhmedEssawy@gmail.com
 * http://fci-h.blogspot.com
 */
/// <summary>
/// Summary description for Class1
/// </summary>
public class GmailHandler
{
    private string username;
    private string password;
    private string gmailAtomUrl;
    private string proxy;

    public string GmailAtomUrl
    {
        get { return gmailAtomUrl; }
        set { gmailAtomUrl = value; }
    }

    public string Password
    {
        get { return password; }
        set { password = value; }
    }

    public string Username
    {
        get { return username; }
        set { username = value; }
    }

    public string Proxy
    {
        get { return proxy; }
        set { proxy = value; }
    }

    public GmailHandler(string _Username, string _Password, string _GmailAtomUrl, string _proxy = null)
    {
        Username = _Username;
        Password = _Password;
        GmailAtomUrl = _GmailAtomUrl;
        Proxy = _proxy;
    }

    public GmailHandler(string _Username, string _Password, string _proxy = null)
    {
        Username = _Username;
        Password = _Password;
        GmailAtomUrl = "https://mail.google.com/mail/feed/atom";
        Proxy = _proxy;
    }
    public XmlDocument GetGmailAtom()
    {
        byte[] buffer = new byte[8192];
        int byteCount = 0;
        XmlDocument _feedXml = null;
        try
        {
            System.Text.StringBuilder sBuilder = new System.Text.StringBuilder();
            WebRequest webRequest = WebRequest.Create(GmailAtomUrl);

            if(!String.IsNullOrWhiteSpace(Proxy))
            webRequest.Proxy = new WebProxy(Proxy, true);

            webRequest.PreAuthenticate = true;

            System.Net.NetworkCredential credentials = new NetworkCredential(this.Username, this.Password);
            webRequest.Credentials = credentials;

            WebResponse webResponse = webRequest.GetResponse();
            Stream stream = webResponse.GetResponseStream();

            while ((byteCount = stream.Read(buffer, 0, buffer.Length)) > 0)
                sBuilder.Append(System.Text.Encoding.ASCII.GetString(buffer, 0, byteCount));


            _feedXml = new XmlDocument();
            _feedXml.LoadXml(sBuilder.ToString());


        }
        catch (Exception ex)
        {
            //add error handling
            throw ex;
        }
        return _feedXml;
    }
}

在您的控制台应用程序中使用它:

        //Create the object from GmailHandler class
        GmailHandler gmailFeed = new GmailHandler("username", "password", "http://proxyserver:80/");            

        //get the feed
        XmlDocument myXml = gmailFeed.GetGmailAtom();


        XmlTextWriter writer = new XmlTextWriter("data.xml", null);
        writer.Formatting = Formatting.Indented;
        myXml.Save(writer);
于 2013-08-30T16:18:40.570 回答