简体   繁体   中英

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. 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

 {
    "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.

There's nothing special about the body of an HTTP POST, so just send that up like you would anything else. 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. 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. I've done this with Go and node.js, and both have simple ways of sending arbitrary data in a POST request body.

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]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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