Imgur es una página web de subir una imagen que ofrece una API to uploadCarga en imgur.com
Mi código es exactamente igual que el código PHP que proporcionan como un ejemplo. sin embargo, en su código php son http_build_query($pvars);
Parece que están URLEncoding su consulta antes de publicarla. editar: Tenga en cuenta que he cambiado a .NET 3.5 completo en lugar del perfil del cliente. Esto me dio acceso a system.web
, así que usé httputliity.urlencode()
. Esto hizo que la API devolviera un "error" con una "no se envió ninguna imagen". Si no codifico, la API devuelve un "OK" con un enlace a la imagen, sin embargo, no se carga ninguna imagen (como un archivo en blanco).
¿Cómo puedo corregir mi código para que funcione correctamente en su API?
Image image = Image.FromFile("C:\\Users\\Affan\\Pictures\\1509310.jpg");
MemoryStream ms = new MemoryStream();
// Convert Image to byte[]
image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
byte[] imageBytes = ms.ToArray();
WebRequest wb = WebRequest.Create(new Uri("http://imgur.com/api/upload.xml"));
wb.ContentType = "application/x-www-form-urlencoded";
wb.Method = "POST";
wb.Timeout = 10000;
Console.WriteLine(imageBytes.Length);
string parameters = "key=433a1bf4743dd8d7845629b95b5ca1b4&image=" + Convert.ToBase64String(imageBytes);
Console.WriteLine("parameters: " + parameters.Length);
System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
byte[] bytes = encoding.GetBytes(parameters);
// byte[] bytes = Convert.FromBase64String(parameters);
System.IO.Stream os = null;
try { // send the Post
wb.ContentLength = bytes.Length; //Count bytes to send
os = wb.GetRequestStream();
os.Write(bytes, 0, bytes.Length); //Send it
} catch (WebException ex) {
MessageBox.Show(ex.Message, "HttpPost: Request error");
Console.WriteLine(ex.Message);
} finally {
if (os != null) {
// os.Close();
}
}
try { // get the response
WebResponse webResponse = wb.GetResponse();
StreamReader sr = new StreamReader(webResponse.GetResponseStream());
//MessageBox.Show(sr.ReadToEnd().Trim());
Console.WriteLine(sr.ReadToEnd().Trim());
} catch (WebException ex) {
MessageBox.Show(ex.Message, "HttpPost: Response error");
}
https://www.youtube.com/watch?v=XZxzJGgox_E -> Por favor no ponga de su aplicación clave y la identificación con su código. – xoxel