将图片上传到 Picasa 网络
我正在尝试使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题很可能出在您阅读图像的方式上。不要将其作为字符串读取,而是尝试将其直接写入请求流,类似于以下内容:
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: