[英]Invalid URI: The Uri string is too long using Imgur API
I am using imgur API to upload images but i am getting an exception on this line: 我正在使用imgur API上传图像,但是在此行出现异常:
string uploadRequestString = "image=" + Uri.EscapeDataString(Convert.ToBase64String(imageData)) + "&key=" + apiKey;
Invalid URI: The Uri string is too long.
无效的URI:Uri字符串太长。
Full code: 完整代码:
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(Convert.ToBase64String(imageData)) + "&key=" + apiKey;
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create("http://api.imgur.com/2/upload");
webRequest.Method = "POST";
webRequest.ContentType = "application/x-www-form-urlencoded";
webRequest.ServicePoint.Expect100Continue = false;
StreamWriter streamWriter = new StreamWriter(webRequest.GetRequestStream());
streamWriter.Write(uploadRequestString);
streamWriter.Close();
WebResponse response = webRequest.GetResponse();
Stream responseStream = response.GetResponseStream();
StreamReader responseReader = new StreamReader(responseStream);
string responseString = responseReader.ReadToEnd();
XmlDocument doc = new XmlDocument();
doc.InnerXml = responseString;
XmlElement root = doc.DocumentElement;
responseString = root.GetElementsByTagName("original")[0].InnerText;
return responseString;
}
It works for smaller size file but getting that error on large files. 它适用于较小的文件,但在较大的文件上会出现该错误。
I suspect that the output from System.Convert.ToBase64String(imageData)
is too long to be a valid URI which is around 2000 characters (I think it's 2048). 我怀疑
System.Convert.ToBase64String(imageData)
的输出太长,无法成为大约2000个字符的有效URI(我认为是2048)。
This will be related to the size of the image as a smaller image can be encoded into a shorter string. 这将与图像的大小有关,因为可以将较小的图像编码为较短的字符串。
You aren't going to be able to get round this limit. 您将无法绕过此限制。
Use a shorter URL. 使用较短的URL。 The URLL per specification is limited and uploading binary data WILL blow this length.
每个规范的URLL受到限制,并且上传二进制数据将超出此长度。
Noone does it - normally data is attached as a form request variables, no part of the URL. 没有人做-通常数据是作为表单请求变量附加的,而不是URL的一部分。
"image=" + Uri.EscapeDataString(System.Convert.ToBase64String(imageData))
“ image =” + Uri.EscapeDataString(System.Convert.ToBase64String(imageData))
WILL NOT WORK. 不管用。 You can not have multi megaby URL's.
您不能有多个兆字节URL。
What is the maximum length of a URL in different browsers? 不同浏览器中URL的最大长度是多少?
has a discussion. 有讨论。 Conclusion is that about 2000 characters is maximum length.
结论是最大长度约为2000个字符。
Anyhow, put the image into a variable that is part of the request payload, not the url. 无论如何,将图像放入请求有效负载(而不是url)的一部分中的变量。
Easy with http://restsharp.org/ 通过http://restsharp.org/轻松
FileStream fileStream = File.OpenRead(imagFilePath);
imageData = new byte[fileStream.Length];
fileStream.Read(imageData, 0, imageData.Length);
fileStream.Close();
RestClient client = new RestClient("https://api.imgur.com/3/image");
RestRequest request = new RestRequest()
{
Method = Method.POST,
};
request.AddHeader("Authorization", "YOUR Client-ID");
request.AddParameter("image", Convert.ToBase64String(imageData), ParameterType.RequestBody);
IRestResponse restResponse = client.Execute(request);
如果可能,您可以使用PUT请求。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.