【问题标题】:Trying to Save Gmail Atom Feed to XML - C#尝试将 Gmail Atom 提要保存到 XML - C#
【发布时间】:2017-05-10 06:52:23
【问题描述】:

我是 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) 需要代理身份验证。”

任何建议将不胜感激!

【问题讨论】:

  • 你需要一个 webclient 或一个 http 请求。只是猜测......

标签: c# visual-studio-2010 gmail atom-feed


【解决方案1】:

我已经尝试了代码,它运行良好(但我的网络中没有代理)。

我已更改 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;
    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);

【讨论】:

  • 我似乎走得更远了,但我仍然遇到了障碍,我认为这是因为代理本身需要凭据。我一直在四处寻找,看看我怎样才能让它通过。感谢您的帮助,非常感谢!
  • 我发现了一个帖子:stackoverflow.com/questions/16420552/…
  • 这应该有帮助:WebProxy webProxy = new WebProxy("myproxy.net:8080", true) { Credentials = new NetworkCredential("username", "pw"), UseDefaultCredentials = false }; request.Proxy = webProxy;
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-02-12
  • 1970-01-01
  • 2011-03-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-12
相关资源
最近更新 更多