我想做的是使用他们的 API 将照片上传到 imgur 网站http://api.imgur.com/endpoints/image#image-upload。正如我在文档中所读到的,图像数据必须在“图像”参数中。所以我正在做的是将 POST 请求数据设置为“image=base64codedfile&title=blabla&type=base64”,它确实上传了它,但文件已损坏。如果我的请求将只包含“base64codedfile”而没有图像、标题、类型名称参数,它的工作就像一个魅力。难道我做错了什么?
如果我将其设置为:
string postData = "image="+Convert.ToBase64String(image)+"&type=base64&title=test;
图像已损坏
如果它只是原始数据:
string postData = Convert.ToBase64String(image);
它正在工作
整个代码是这样的:
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://api.imgur.com/3/upload.xml");
request.Headers.Add("Authorization", "Client-ID >>myclientid<<");
request.Method = "POST";
string filePath = "d:\\test.jpg";
FileStream file = new FileStream(filePath, FileMode.Open);
byte[] image = new byte[file.Length];
file.Read(image, 0, (int)file.Length);
ASCIIEncoding enc = new ASCIIEncoding();
string postData = Convert.ToBase64String(image);
byte[] bytes = enc.GetBytes(postData);
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = bytes.Length;
Stream writer = request.GetRequestStream();
writer.Write(bytes, 0, bytes.Length);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();