문제

I'm new to C# and I'm having some difficulty with saving the XML Atom feed from Gmail to an xml file. I'm certain I'm miles off of where I need to be and I'm embarassed to be asking this but I'm not getting anywhere on my own:(

I'm using the GmailHandler class that's been floating around for some time.

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

}

Then I've got my Program.cs here:

I'm assuming the issue is with the code below since I'm responsible for that, and not what's above.

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




        }
    }
}

When I run the program I get a "WebException was unhandled - The remote server returned an error: (407) Proxy Authentication Required."

enter image description here

Any advice would be appreciated!

도움이 되었습니까?

해결책

I have tried the code and it works fine (but I have no proxy in my network).

I have changed the GmailHandler.cs, the constructor now accepts a internet proxy.

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

Use this in your console application:

        //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);
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top