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 was unhandled - The remote server returned an error: (407) Proxy Authentication Required.」というメッセージが表示されます。

ここに画像の説明を入力

アドバイスをいただければ幸いです。

4

1 に答える 1

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);
于 2013-08-30T16:18:40.570 に答える