将图片上传到 picasa 网络 [英] Uploading picture to picasa web

查看:29
本文介绍了将图片上传到 picasa 网络的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

我的代码:

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

谢谢

推荐答案

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

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

}

这篇关于将图片上传到 picasa 网络的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆