Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
试图将Gmail Atom提要保存到XML-C#_C#_Visual Studio 2010_Gmail_Atom Feed - Fatal编程技术网

试图将Gmail Atom提要保存到XML-C#

试图将Gmail Atom提要保存到XML-C#,c#,visual-studio-2010,gmail,atom-feed,C#,Visual Studio 2010,Gmail,Atom Feed,我是C#新手,在将Gmail中的XML Atom提要保存到XML文件时遇到了一些困难。我肯定我离我该去的地方还有很远的路,我很难为情地问这个问题,但我一个人是不会去任何地方的:( 我使用的Gmail处理程序类已经运行了一段时间 GmailHandler.cs using System; using System.Data; using System.Xml; using System.Net; using System.IO; /* * this code made by Ahmed Essa

我是C#新手,在将Gmail中的XML Atom提要保存到XML文件时遇到了一些困难。我肯定我离我该去的地方还有很远的路,我很难为情地问这个问题,但我一个人是不会去任何地方的:(

我使用的Gmail处理程序类已经运行了一段时间

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;
    }

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


如果您有任何建议,我们将不胜感激!

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

我已经更改了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;
    }
}

你需要一个网络客户端或http请求。尽管只是一个猜测…我似乎走得更远了,但我仍然遇到了路障,我认为这是因为代理本身需要凭据。我一直在四处寻找,看看如何才能通过。谢谢你的帮助,非常感谢!我找到了一篇关于这一点的帖子:This应该有帮助:WebProxy WebProxy=new WebProxy(“,true){Credentials=new NetworkCredential(“username”,“pw”),UseDefaultCredentials=false};request.Proxy=WebProxy;
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);