简体   繁体   English

如何通过POST向服务器提交一个非常长的字符串并获得jSON响应

[英]How to submit a very-very long String to server via POST and get jSON response

I wish to send a very very long string(length of string is more than 10000) to the server and in return get the jSON response from the string.What is the best approach for the task.我希望向服务器发送一个非常长的字符串(字符串长度超过 10000),作为回报,从字符串中获取 json 响应。该任务的最佳方法是什么。 I am sending various parameters along with this very very long string.我正在发送各种参数以及这个非常长的字符串。

Split down your long string to parts which can be send over one request.将您的长字符串拆分为可以通过一个请求发送的部分。 Create a json like this像这样创建一个json

 {
    "index":"0",
    "length":"LENGTH_OF_STRING",
    "string":"xsfsffwff.......",
    //other json parameters
 }

then you can send your string然后你可以发送你的字符串

The problem is that you're trying to put this all into a query parameter.问题是您试图将所有这些都放入查询参数中。 Most servers have built-in limits for URLs, and for good reason.大多数服务器都有内置的 URL 限制,这是有充分理由的。

There's nothing special about the body of an HTTP POST, so just send that up like you would anything else. HTTP POST 的正文没有什么特别之处,因此只需像发送其他任何内容一样发送它即可。 Just make sure that you set the Content-Length header (since you know that; it might be covered by the HTTP library) and then just stream your data up.只需确保您设置了Content-Length标头(因为您知道这一点;它可能被 HTTP 库覆盖),然后将您的数据流式传输。 No need for any encoding or query params.不需要任何编码或查询参数。

I don't know much about objective-c, but I'm sure there's a way to send data like this in an HTTP POST very simply.我不太了解objective-c,但我确信有一种方法可以非常简单地在HTTP POST 中发送这样的数据。 I've done this with Go and node.js, and both have simple ways of sending arbitrary data in a POST request body.我已经用 Go 和 node.js 做到了这一点,它们都有在 POST 请求正文中发送任意数据的简单方法。

If you are using the ASI-Http classes , then you can send request like this
  ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:[WebService getAddPhoto]]];
                 [request addPostValue:[[imgArray objectAtIndex:i] valueForKey:@"vComments"] forKey:@"comment"];
                 NSData *imageData = UIImagePNGRepresentation([UIImage imageWithContentsOfFile:[[imgArray objectAtIndex:i] valueForKey:@"vPhoto"]]);
                 NSString *encodedString = [imageData base64EncodingWithLineLength:[imageData length]];
                 [request addPostValue:encodedString forKey:@"Photo"];
                 [request setDelegate:self];
                [request startAsynchronous]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM