上传图片到imgur原因:Uri字符串过长

本文关键字:Uri 字符串 原因 imgur | 更新日期: 2023-09-27 18:11:19

我正在尝试开发使用Imgur api上传图片的c#应用程序。

这是我的上传功能:

    public static string PostToImgur(string imagFilePath, string apiKey)
{
    byte[] imageData;
    FileStream fileStream = File.OpenRead(imagFilePath);
    imageData = new byte[fileStream.Length];
    fileStream.Read(imageData, 0, imageData.Length);
    fileStream.Close();
    string uploadRequestString = "image=" + Uri.EscapeDataString(System.Convert.ToBase64String(imageData)) + "&key=" + apiKey;
    HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create("https://api.imgur.com/3/image");
    webRequest.Method = "POST";
    webRequest.ContentType = "application/x-www-form-urlencoded";
    webRequest.ServicePoint.Expect100Continue = false;
    webRequest.Headers["Authorization"] = "Client-ID abd937cc5e11dc9";
    StreamWriter streamWriter = new StreamWriter(webRequest.GetRequestStream());
    streamWriter.Write(uploadRequestString);
    streamWriter.Close();
    WebResponse response = webRequest.GetResponse();
    Stream responseStream = response.GetResponseStream();
    StreamReader responseReader = new StreamReader(responseStream);
    return responseReader.ReadToEnd();
}

如果我上传小图片(20kb),所有工作都很好。但是当我上传更大的图像(500kb)时,我得到错误:无效URI: URI字符串太长。

我能做什么?

上传图片到imgur原因:Uri字符串过长

通过划分base64字符串成功上传大图

这是工作代码:

    public static string PostToImgur(string imagFilePath, string apiKey)
{
    byte[] imageData;
    FileStream fileStream = File.OpenRead(imagFilePath);
    imageData = new byte[fileStream.Length];
    fileStream.Read(imageData, 0, imageData.Length);
    fileStream.Close();
    const int MAX_URI_LENGTH = 32766;
    string base64img = System.Convert.ToBase64String(imageData);
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < base64img.Length; i += MAX_URI_LENGTH)
    {
        sb.Append(Uri.EscapeDataString(base64img.Substring(i, Math.Min(MAX_URI_LENGTH, base64img.Length - i))));
    }
    string uploadRequestString = "key=" + apiKey + "&title=" + "imageTitle" +
        "&caption=" + "img" + "&image=" + sb.ToString();
   // string uploadRequestString = "image=" + Uri.EscapeDataString(System.Convert.ToBase64String(imageData)) + "&key=" + apiKey;
    HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create("https://api.imgur.com/3/image");
    webRequest.Method = "POST";
    webRequest.ContentType = "application/x-www-form-urlencoded";
    webRequest.ServicePoint.Expect100Continue = false;
    webRequest.Headers["Authorization"] = "Client-ID abd937cc5e11dc9";
    StreamWriter streamWriter = new StreamWriter(webRequest.GetRequestStream());
    streamWriter.Write(uploadRequestString);
    streamWriter.Close();
    WebResponse response = webRequest.GetResponse();
    Stream responseStream = response.GetResponseStream();
    StreamReader responseReader = new StreamReader(responseStream);
    return responseReader.ReadToEnd();
}

感谢大家。

他们的工作方式是将图像转换为:System.Convert.ToBase64String(imageData)将其添加到URI中并发送到服务器以解码回图像。

你将无法通过一个单一的URI来解决这个问题,因为这是imgur的设计固有的。

在不同的浏览器中,URL的最大长度是多少?