[英]String too large for FormUrlEncodedContent() in Windows Phone 8 Post upload
My code: 我的代码:
string json = BuildJson(uploadItem);
using (var client = new HttpClient())
{
var values = new List<KeyValuePair<string, string>>();
values.Add(new KeyValuePair<string, string>("parameter", json));
var content = new FormUrlEncodedContent(values);
var response = await client.PostAsync(App.Current.LoginUrl, content);
var responseString = await response.Content.ReadAsStringAsync();
}
My json string includes an base64 encoded image so the FormUrlEncodedContent
throws the exception : 我的json字符串包含base64编码的图像,因此
FormUrlEncodedContent
引发异常:
" Invalid URI: The Uri string is too long".
“无效的URI:Uri字符串太长”。
Important is that the server expects exact this format with "parameter" as post key and the json as the post value. 重要的是服务器需要使用“ parameter”作为发布键,并使用json作为发布值的确切格式。 How can I bypass this limitation of
FormUrlEncodedContent
? 如何绕过
FormUrlEncodedContent
限制?
I solved this issue with the following method which replaces FormUrlEncodedContent : 我用以下替代FormUrlEncodedContent的方法解决了这个问题:
// URI Escape JSON string
var content = EscapeDataString(json);
private string EscapeDataString(string str)
{
int limit = 2000;
StringBuilder sb = new StringBuilder();
int loops = str.Length / limit;
for (int i = 0; i <= loops; i++)
{
if (i < loops)
{
sb.Append(Uri.EscapeDataString(str.Substring(limit * i, limit)));
}
else
{
sb.Append(Uri.EscapeDataString(str.Substring(limit * i)));
}
}
return sb.ToString();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.