正在上传照片到 vk.com api 服务器
Uploading a photo to the vk.com api server
我正在尝试将图像上传到 vk.com 的服务器,但根据我的请求 - 我得到一个空 "photo":""[]";
{"server":627329, "photo":"[]","hash":"6ce9e707ba60a464bc45070a748dc9ec "}
我的代码是:
private static HttpWebResponse PostMethod()
{
//getting json with url to upload.
string url = "https://api.vk.com/method/photos.getMessagesUploadServer?&v=5.31&access_token=247878418f7f0ab793cd40e1434af3f51794ec09e85";
WebClient client = new WebClient();
string json = client.DownloadString(url);
JavaScriptSerializer json_serializer = new JavaScriptSerializer();
//getting an image for uploading later.
byte[] imageData = client.DownloadData("http://cs5530.vk.me/u43529379/-6/m_b9515ce2.jpg");
RootObject response = (RootObject)json_serializer.Deserialize(json, typeof(RootObject));
//Geting out url for uploading, from response.
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(response.response.upload_url);
request.Method = "POST";
request.Credentials = CredentialCache.DefaultCredentials;
UTF8Encoding encoding = new UTF8Encoding();
var bytes = encoding.GetBytes(imageData.ToString());
request.ContentType = "application/json";
request.ContentLength = bytes.Length;
using (var newStream = request.GetRequestStream())
{
newStream.Write(bytes, 0, bytes.Length);
newStream.Close();
}
return (HttpWebResponse)request.GetResponse();
}
如有任何建议,我们将不胜感激。谢谢。
看到您发布的文档后
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(response.response.upload_url);
request.Method = "POST";
request.Credentials = CredentialCache.DefaultCredentials;
request.TransferEncoding = "utf8";
UTF8Encoding encoding = new UTF8Encoding();
string encoded = Convert.ToBase64String(imageData);
string postData = "photo=" + encoded;
request.ContentType = "multipart/form-data";
request.ContentLength = Encoding.UTF8.GetByteCount(postData);
using (var newStream = request.GetRequestStream())
{
byte[] postBytes = Encoding.UTF8.GetBytes(postData);
newStream.Write(postBytes, 0, postBytes.Length);
newStream.Close();
}
return (HttpWebResponse)request.GetResponse();
我正在尝试将图像上传到 vk.com 的服务器,但根据我的请求 - 我得到一个空 "photo":""[]";
{"server":627329, "photo":"[]","hash":"6ce9e707ba60a464bc45070a748dc9ec "}
我的代码是:
private static HttpWebResponse PostMethod()
{
//getting json with url to upload.
string url = "https://api.vk.com/method/photos.getMessagesUploadServer?&v=5.31&access_token=247878418f7f0ab793cd40e1434af3f51794ec09e85";
WebClient client = new WebClient();
string json = client.DownloadString(url);
JavaScriptSerializer json_serializer = new JavaScriptSerializer();
//getting an image for uploading later.
byte[] imageData = client.DownloadData("http://cs5530.vk.me/u43529379/-6/m_b9515ce2.jpg");
RootObject response = (RootObject)json_serializer.Deserialize(json, typeof(RootObject));
//Geting out url for uploading, from response.
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(response.response.upload_url);
request.Method = "POST";
request.Credentials = CredentialCache.DefaultCredentials;
UTF8Encoding encoding = new UTF8Encoding();
var bytes = encoding.GetBytes(imageData.ToString());
request.ContentType = "application/json";
request.ContentLength = bytes.Length;
using (var newStream = request.GetRequestStream())
{
newStream.Write(bytes, 0, bytes.Length);
newStream.Close();
}
return (HttpWebResponse)request.GetResponse();
}
如有任何建议,我们将不胜感激。谢谢。
看到您发布的文档后
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(response.response.upload_url);
request.Method = "POST";
request.Credentials = CredentialCache.DefaultCredentials;
request.TransferEncoding = "utf8";
UTF8Encoding encoding = new UTF8Encoding();
string encoded = Convert.ToBase64String(imageData);
string postData = "photo=" + encoded;
request.ContentType = "multipart/form-data";
request.ContentLength = Encoding.UTF8.GetByteCount(postData);
using (var newStream = request.GetRequestStream())
{
byte[] postBytes = Encoding.UTF8.GetBytes(postData);
newStream.Write(postBytes, 0, postBytes.Length);
newStream.Close();
}
return (HttpWebResponse)request.GetResponse();