将图片上传到 Picasa 网络

发布于 2024-11-18 08:44:37 字数 1171 浏览 3 评论 0原文

我正在尝试使用 API 将新照片上传到 Picasa。 代码不工作。 我收到以下错误:

异常详细信息:System.Net.WebException:远程服务器返回错误:(400) 错误请求。

我的代码:

string imgPath = "C:\foo.png"; 
StreamReader reader = new StreamReader(imgPath); 
string imgBin = reader.ReadToEnd(); 
reader.Close();
string id=""//picasa ID
string album = "";//album name
string url = String.Format("http://www.picasaweb.google.com/data/feed/api/user/{0}/album/{1}",id, album);
string auth = "";

        Byte[] send = Encoding.UTF8.GetBytes(imgBin); 
        int length = send.Length;
        HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(url);
        req.Method = "POST";
        req.ContentType = "image/png";
        req.ContentLength = length;
        req.Headers.Add("Authorization", "GoogleLogin auth=" + auth);
        req.Headers.Add("Slug", "test");
        Stream stream = req.GetRequestStream();
        stream.Write(send, 0, length);
        stream.Close();
        WebResponse response = req.GetResponse();
        StreamReader reader = new StreamReader(response.GetResponseStream());
        string res = reader.ReadToEnd();
        reader.Close();

I m trying to upload a new photo to Picasa using the API.
code not working.
I am getting the following error:

Exception Details: System.Net.WebException: The remote server returned an error: (400) Bad Request.

My code:

string imgPath = "C:\foo.png"; 
StreamReader reader = new StreamReader(imgPath); 
string imgBin = reader.ReadToEnd(); 
reader.Close();
string id=""//picasa ID
string album = "";//album name
string url = String.Format("http://www.picasaweb.google.com/data/feed/api/user/{0}/album/{1}",id, album);
string auth = "";

        Byte[] send = Encoding.UTF8.GetBytes(imgBin); 
        int length = send.Length;
        HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(url);
        req.Method = "POST";
        req.ContentType = "image/png";
        req.ContentLength = length;
        req.Headers.Add("Authorization", "GoogleLogin auth=" + auth);
        req.Headers.Add("Slug", "test");
        Stream stream = req.GetRequestStream();
        stream.Write(send, 0, length);
        stream.Close();
        WebResponse response = req.GetResponse();
        StreamReader reader = new StreamReader(response.GetResponseStream());
        string res = reader.ReadToEnd();
        reader.Close();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

裂开嘴轻声笑有多痛 2024-11-25 08:44:37

问题很可能出在您阅读图像的方式上。不要将其作为字符串读取,而是尝试将其直接写入请求流,类似于以下内容:

using (Stream fileStream = new FileStream(imgPath, FileMode.Open, FileAccess.Read))
{
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
    request.Method = "POST";
    request.ContentType = "image/png";
    request.ContentLength = fileStream.Length;
    request.Headers.Add(HttpRequestHeader.Authorization, "GoogleLogin auth=" + auth);
    request.Headers.Add("Slug", "test");

    using (Stream requestStream = request.GetRequestStream())
    {
        byte[] buffer = new byte[4096];
        int bytesRead = 0;
        while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
        {
            requestStream.Write(buffer, 0, bytesRead);
        }

        fileStream.Close();
        requestStream.Close();
    }

    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
    StreamReader responseReader = new StreamReader(response.GetResponseStream());

    string responseStr = responseReader.ReadToEnd();

}

The problem is most likely with how you are reading the image. Instead of reading it as a string, try writing it directly into the request stream, similar to the following:

using (Stream fileStream = new FileStream(imgPath, FileMode.Open, FileAccess.Read))
{
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
    request.Method = "POST";
    request.ContentType = "image/png";
    request.ContentLength = fileStream.Length;
    request.Headers.Add(HttpRequestHeader.Authorization, "GoogleLogin auth=" + auth);
    request.Headers.Add("Slug", "test");

    using (Stream requestStream = request.GetRequestStream())
    {
        byte[] buffer = new byte[4096];
        int bytesRead = 0;
        while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
        {
            requestStream.Write(buffer, 0, bytesRead);
        }

        fileStream.Close();
        requestStream.Close();
    }

    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
    StreamReader responseReader = new StreamReader(response.GetResponseStream());

    string responseStr = responseReader.ReadToEnd();

}
无远思近则忧 2024-11-25 08:44:37
            string username = form["UserName"].ToString(); // <-- ### USERNAME HERE ###

            string password = form["Password"].ToString(); // <-- ### PASSWORD HERE ###
            PicasaService picasaService = new PicasaService("Tester");
            picasaService.setUserCredentials(username, password);

            // 2. Create a test album
            //
            AlbumEntry entry = new AlbumEntry();
            entry.Title.Text = "test-69";
            entry.Summary.Text = "test-69";
            AlbumAccessor access = new AlbumAccessor(entry);
            PicasaEntry album = picasaService.Insert(new Uri(PicasaQuery.CreatePicasaUri(username)), entry);

            access = new AlbumAccessor(album);

            // 3. Upload a photo
            picasaService.Insert(new Uri(PhotoQuery.CreatePicasaUri(username, access.Id)), System.IO.File.OpenRead("thumb-1.jpg"), "image/jpeg", "thumb-1.jpg");
            string username = form["UserName"].ToString(); // <-- ### USERNAME HERE ###

            string password = form["Password"].ToString(); // <-- ### PASSWORD HERE ###
            PicasaService picasaService = new PicasaService("Tester");
            picasaService.setUserCredentials(username, password);

            // 2. Create a test album
            //
            AlbumEntry entry = new AlbumEntry();
            entry.Title.Text = "test-69";
            entry.Summary.Text = "test-69";
            AlbumAccessor access = new AlbumAccessor(entry);
            PicasaEntry album = picasaService.Insert(new Uri(PicasaQuery.CreatePicasaUri(username)), entry);

            access = new AlbumAccessor(album);

            // 3. Upload a photo
            picasaService.Insert(new Uri(PhotoQuery.CreatePicasaUri(username, access.Id)), System.IO.File.OpenRead("thumb-1.jpg"), "image/jpeg", "thumb-1.jpg");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文