Re: Cocoa: How do I send/receive XML within a HTTP connect? -- Solved.

  • I got this to work!

    The following is the complete code.
    All the authorization and data is supplied by the .xml test file that I have stored within the App bundle.

    Thanks all!

    =========================

    - (IBAction)startSOAP:(id)sender {
        NSLog(@"\n{AppDelegate} startSOAP start");
        // create the request
        NSError **myError;
        NSHTTPURLResponse **serverResponse;
        NSData *serverData;
        NSDictionary *headerFieldsDict = [NSDictionary
                dictionaryWithObjectsAndKeys:@"Apple iPhone",@"User-Agent",
                                          @"text/xml; charset=utf-8", @"Content-Type",
                                          @"soapAction",@"SOAP_ACTION",nil];
        @try {

            // 1) The Request String.
            // Note: smsXMLString contains the entire SMS SOAP envelope, without the <? XML declaration command >.
            NSString *smsXMLPath = [[NSBundle mainBundle] pathForResource:@"sms" ofType:@"xml"];
            self.smsXMLString = [NSString stringWithContentsOfFile:smsXMLPath encoding:NSUTF8StringEncoding error:myError];

            // -----------------------
            // 2)  Create the request.
            NSMutableURLRequest  *theRequest=[NSMutableURLRequest requestWithURL:[NSURL URLWithString:theServerURL]
                                                      cachePolicy:NSURLRequestUseProtocolCachePolicy
                                                  timeoutInterval:10.0];

            // -----------------------
            // 2a)  Modify the Request from default 'GET' to 'POST':
            [theRequest setHTTPMethod:@"POST"];

            // 2b) Modify the Headers:
            [theRequest setAllHTTPHeaderFields:headerFieldsDict];

            // 2c) Sent the Contents of the Body to the SOAP/XML data:
            [theRequest setHTTPBody:[self.smsXMLString dataUsingEncoding:NSUTF8StringEncoding]];
            // -----------------------
            // 3)  Get Synchronous Data:
            serverData = [NSURLConnection sendSynchronousRequest:theRequest
                                              returningResponse:serverResponse
                                                          error:myError];

            // -----------------------
            // 4)  Convert Synchronous Data into Human-Readable String (Unicode 8) format:
            NSString *serverDataString = [[[NSString alloc] initWithData:serverData encoding:NSUTF8StringEncoding] retain];

            [[soapResponse layoutManager]replaceTextStorage:[[NSTextStorage alloc] initWithString:serverDataString]];

            [serverDataString release];

        } @catch (id e) {
            NSLog(@"\n**** {startSOAP} EXCEPTION: %@ ****\n",e);
            self.statusLine.stringValue = [NSString stringWithFormat:@"*** Exception flagged: %@ ***",e];
        } @finally {

            NSLog(@"\n{startSoap} end.");
        }


    } // end startSOAP().

    =========================

    On 05/16/2008 12:20 Jens Alfke wrote ..
    >
    > On 16 May '08, at 11:11 AM, <fclee...> wrote:
    >
    >> Via the NSMutableURLRequest class, I can alter the HTTP Body, etc.
    >> However, I need to send a SOAP/XML envelope & receive a XML response.
    >
    > NSURLRequest etc. have nothing to do with the type of content you
    > send. They don't care whether it's XML, HTML, MP3, JPEG, whatever. You
    > just hand them raw data to send in the body.
    >
    >> I don't see where in the NSNetworking docs that I can send a SOAP/
    >> XML to the server.
    >
    > That's too bad. If only the documentation came with some kind of
    > "search" feature so you didn't have to ask hundreds of people for help
    > on every little detail. Then you'd be able to type "SOAP" into the
    > search field, maybe adjust the settings to "Full Text" and "Contains",
    > and find that documentation wherever it was! But since Xcode sucks and
    > has no search feature, and since Apple won't let Google index their
    > online docs, there's no way around it. Someone else will have to take
    > over answering, though, because I'm getting tired of it.
    >
    > —Jens
  • On 16 May '08, at 1:36 PM, <fclee...> wrote:

    > I got this to work!
    ...
    > NSError **myError;
    > NSHTTPURLResponse **serverResponse;
    ...
    > self.smsXMLString = [NSString
    > stringWithContentsOfFile:smsXMLPath encoding:NSUTF8StringEncoding
    > error:myError];

    If that's not crashing, it's only because it never got an HTTP error.
    Someone else already pointed out to you that the those types are
    wrong. What your code will do if an error occurs is try to write the
    NSError to a random memory location, either crashing or corrupting
    your state. Go and look up the earlier message if you want to see how
    to fix it.

    —Jens
previous month may 2008 next month
MTWTFSS
      1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31  
Go to today