Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6608039
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T19:34:03+00:00 2026-05-25T19:34:03+00:00

I have code that sends away a HTTP POST connection. I want the method

  • 0

I have code that sends away a HTTP POST connection. I want the method to wait until I get a response from the server for it to continue. The reason I am doing it this way is because I am integrating new code (asynchronous post vs the old synchronous post) into our application and I am looking for minimal change throughout the application.

The old method was as follows:

-(NSData*) postData: (NSString*) strData;

The application would call it and send it a strData object and it would lock the main thread until it got something back. This was inefficient but it worked well, but due to timeout constraints I have to change it.

So my new method (posting the complete method here) is as follows:

-(NSData*) postData: (NSString*) strData
{
    //start http request code
    //postString is the STRING TO BE POSTED
    NSString *postString;
    //this is the string to send
    postString = @"data=";
    postString = [postString stringByAppendingString:strData]; 
    NSURL *url = [NSURL URLWithString:@"MYSERVERURL"];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    NSString *msgLength = [NSString stringWithFormat:@"%d", [postString length]];
    //setting prarameters of the POST connection
    [request setHTTPMethod:@"POST"];
    [request addValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    [request addValue:msgLength forHTTPHeaderField:@"Content-Length"];
    [request addValue:@"en-US" forHTTPHeaderField:@"Content-Language"];
    [request setHTTPBody:[postString dataUsingEncoding:NSUTF8StringEncoding]];
    [request setTimeoutInterval:20]; //one second for testing purposes
    NSLog(@"%@",postString);    
    NSURLConnection *connection = [NSURLConnection connectionWithRequest:request delegate:self];
    [connection start];
    //end http request code
    return receivedData; //this is set by a delegate somewhere else in the code
}

It sends the code fine, but of course (and as expected), it does not receive it fast enough to be returned correctly.

What do you recommend I can do to “stop” that method to wait to return anything until something is received? I’ve tried setting a while loop that waited on a BOOL that would be set to YES when all data was received, but that loop prevented the code from sending at all. I also tried throwing the contents of this method into another method and calling it to performSelectorInBackground, but of course, that didn’t work either. I’m running out of ideas and I’d really appreciate the help.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T19:34:03+00:00Added an answer on May 25, 2026 at 7:34 pm

    Doing any sort of synchronous comms on the main thread is a bad idea, but if you can’t re-architect at this point then take a look at:

    +[NSURLConnection sendSynchronousRequest:returningResponse:error:]

    The documentation can be found here. From the discussion:

    A synchronous load is built on top of the asynchronous loading code
    made available by the class. The calling thread is blocked while the
    asynchronous loading system performs the URL load on a thread spawned
    specifically for this load request. No special threading or run loop
    configuration is necessary in the calling thread in order to perform a
    synchronous load.

    But seriously, take a look and how much work would be involved in performing the request asynchronously and receiving a notification when the request is complete. The whole experience for the user is going to be much better.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have code that sends emails to users from a signup webpage. Occasionally, users
I have the following code that works on PHP5 to send a HTTP POST
I have code that I want to look like this: List<Type> Os; ... foreach
I have code that sends a message into a MessageQueue . _queue.Send(new Message(myData)); This
I have some code in a javascript file that needs to send queries back
I have code that references a web service, and I'd like the address of
I have code that looks like the following, which works fine for displaying the
I have code that looks like: //System.Data.IDataRecord dr try { Consolidated = Utility.NullConvert.ToBool(dr[Constants.Data.Columns.cConsolidated], false);
I have code that looks like this: template<class T> class list { public: class
I have code that uses Win API function RegSaveKeyEx to save registry entries to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.