【问题标题】:Send large JSON string发送大型 JSON 字符串
【发布时间】:2014-07-20 19:28:08
【问题描述】:

我正在尝试使用查询字符串(POST 方法)将我的 c# 应用程序中的大型 json 字符串发送到 asp.net 页面,但是由于字符串太长,它给了我这个消息:无效的 uri:uri 链接太长。

我的问题还有其他解决方案吗!?

if(allRecords.Count > 0)
    for (int j = 0; j < allRecords.Count; j++)
    {
        queryString += JsonConvert.SerializeObject(allRecords[j], Newtonsoft.Json.Formatting.Indented);
    }
ASCIIEncoding encoding = new ASCIIEncoding();
byte[] data = encoding.GetBytes(queryString);

// Set up Request.
HttpWebRequest webReq = WebRequest.Create(onlineApp) as HttpWebRequest;
webReq.ContentType = "text/plain";
webReq.Method = "POST";
webReq.Credentials = CredentialCache.DefaultCredentials;
webReq.ContentLength = data.Length;

// Send Request.


Stream newStream = webReq.GetRequestStream();
newStream.Write(data, 0, data.Length);
newStream.Close();

// get Response.
HttpWebResponse response = (HttpWebResponse)webReq.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream());

string tt = reader.ReadToEnd();

reader.Close();
response.Close();

【问题讨论】:

  • POST 数据不是 URI 的一部分,所以这个错误对我来说没有多大意义。 ASP.NET 页面如何处理 POST 数据?错误会在那里吗?
  • 如何使用WebClient 类和WebClient.UploadData overloads 之一而不是WebRequest 类?
  • 我可能会错过它,但仅基于以上,querystring 是什么?变量名?实际的query string(uri)?我看到前者(你似乎在发布)..但不是后者......
  • Svick ..错误发生在 c# 页面中..在我发送之前!我的查询字符串太长了..我检查了一下。我需要将 json 字符串发送到 asp.net 页面!
  • 我看不到您在哪里创建 URL。 URL 是 onlineApp 变量吗?如果您要发布数据,则无需创建带有查询字符串的 URL。例如com?s=querystring。如果 API 需要 POST,它会将您正在写入的数据带入请求流。

标签: c# asp.net json


【解决方案1】:

我认为您的问题在您的 WebRequest 中,当您创建新的 Webrequest 时,请检查您的 webrequest 字符串。

【讨论】:

    猜你喜欢
    • 2021-08-01
    • 2020-02-17
    • 2019-10-11
    • 2016-11-07
    • 2018-11-24
    • 2015-11-18
    • 1970-01-01
    • 1970-01-01
    • 2017-01-07
    相关资源
    最近更新 更多