c# - Uploading a photo to the vk.com api server -
i'm trying upload image server of vk.com, in result of request - empty "photo":"[]";
{"server":627329, "photo":"[]","hash":"6ce9e707ba60a464bc45070a748dc9ec "}
my code is:
private static httpwebresponse postmethod() { //getting json url 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 image 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 uploading, 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(); }
any advice appreciated. thank you.
after seeing documentation posted
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();
Comments
Post a Comment