Uri.EscapeDataString() - 无效的URI:URI字符串太长 [英] Uri.EscapeDataString() - Invalid URI: The Uri string is too long

查看:2010
本文介绍了Uri.EscapeDataString() - 无效的URI:URI字符串太长的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是Windows Mobile的紧凑型框架/ C#。

I'm using compact framework/C# on windows mobile.

在我的申请,我通过序列化对象和使用的HttpWebRequest / POST请求将数据上传到服务器发送信息了。在服务器POST数据是反序列化并保存到数据库。

In my application I am uploading data to the server by serializing objects and using a HttpWebRequest/POST request to send the information up. On the server the post data is de-serialised and saved to the db.

有一天,我意识到,我曾与在后数据特殊字符的问题(&符号等)。所以我介绍Uri.EscapeDataString()进入方法,一切都很好。

The other day I realised that I had a problem with special characters in the post data (ampersands etc..). So I introduced Uri.EscapeDataString() into the method and all was well.

不过,今天我已经发现,当应用程序试图上载大,有一个问题数据量(我不确定究竟是指在目前大!)

However, today I have discovered that there is a problem when the application attempts to upload a large amount of data (I'm unsure of what exactly denotes "large" at the moment!)

现有代码(种)

var uploadData = new List<Things>();

uploadData.Add(new Thing() { Name = "Test 01" });
uploadData.Add(new Thing() { Name = "Test 02" });
uploadData.Add(new Thing() { Name = "Test with an & Ampersand " }); // Do this a lot!!

var postData = "uploadData=" + Uri.EscapeDataString(JsonConvert.SerializeObject(uploadData, new IsoDateTimeConverter()));



问题

要Uri.EscapeDataString()的调用导致以下异常:

The call to Uri.EscapeDataString() is causing the following exception:

System.UriFormatException:无效的URI:URI字符串太长。

System.UriFormatException: Invalid URI: The Uri string is too long.

是否有任何其他方式来上传准备数据?

Are there any other ways to prepare the data for upload?

据我可以看到HttpUtility(它有自己的编码/解码方法)不适用于紧凑的框架。

As far as I can see HttpUtility (which has its own Encode/Decode methods) is not available for the compact framework.

推荐答案

或者你可以简单地分割你的字符串,并调用 Uri.EscapeDataString(串)对每个块,以避免重新实现功能

Or you could simply split your string and call Uri.EscapeDataString(string) for each block, in order to avoid reimplementing the function.

示例代码:

        String value = "large string to encode";
        int limit = 2000;

        StringBuilder sb = new StringBuilder();
        int loops = value.Length / limit;

        for (int i = 0; i <= loops; i++)
        {
            if (i < loops)
            {
                sb.Append(Uri.EscapeDataString(value.Substring(limit * i, limit)));
            }
            else
            {
                sb.Append(Uri.EscapeDataString(value.Substring(limit * i)));
            }
        }

这篇关于Uri.EscapeDataString() - 无效的URI:URI字符串太长的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆