Send httprequest request

Source: Internet
Author: User

Before developing an applicationProgramEspecially when the current network is particularly adequate and important, the boundaries between network applications and desktop applications have become increasingly blurred. Therefore, the network application can open APIs to the desktop application, and then the desktop application can call the APIs to facilitate the maintenance of a copy.CodeAnd can be pushed to the user.

During the design, the client usually sends the corresponding HTTP request and contains some content. After sending the request, wait for the response from the server. On the server side, you can get the request content and generate corresponding results to return. Although this process is easy to understand and simple, it is still a little troublesome for Mac and iPhone development, and there are few materials in China. So here I will write about how to send httprequest requests, convenience for later students.

This Code is applicable to Mac OS X and iPhone applications.

I will use the Mac desktop application for the interface, because it is simple and the effect is the same, the interface can be made as follows, how to connect yourself to connect, I previouslyArticleI understand it.

When we press the button, we get the request from the corresponding website/server. The call method is as follows.

- (Ibaction) buttonclicked :( ID) sender
{
Nsstring * Receive = [Requestsender sendrequest: @" Http://wt.jguoer.com " ];
Textbox. Title = Receive;
Nslog ( @" Clicked " );
}

Requestsender is a Class I wrote. This class is used to send HTTP requests. The specific code is as follows.

# Import < Cocoa / Cocoa. h >

@ Interface requestsender: nsobject {

}

+(Nsstring*) Sendrequest :( nsstring*) URL;

@ End

The implementation code is as follows. I have already written detailed comments, so I don't need to talk about anything more.

+ (Nsstring * ) Sendrequest :( nsstring * ) URL
{
// Prepare to send httprequest  
Nsstring * Urlstring = URL;
Nsmutableurlrequest * Request = [[[Nsmutableurlrequest alloc] init] autorelease];
[Request seturl: [nsurl urlwithstring: urlstring];
[Request sethttpmethod: @" Get " ];

// Set HTTP Header  
Nsstring * Contenttype = [Nsstring stringwithformat: @" Text/XML " ];
[Request addvalue: contenttype forhttpheaderfield: @" Content-Type " ];

// Create HTTP Content
// Nsmutabledata * postbody = [nsmutabledata data];
// [Postbody appenddata: [[nsstring stringwithformat: @ "<XML>"] datausingencoding: nsutf8stringencoding];
// [Postbody appenddata: [[nsstring stringwithformat: @ "<your XML format code here/>"] // Datausingencoding: nsutf8stringencoding];
// [Postbody appenddata: [[nsstring stringwithformat: @ "</XML>"] datausingencoding: nsutf8stringencoding];

// Set the sent content
// [Request sethttpbody: postbody];

// Get Response  
Nshttpurlresponse * Urlresponse = Nil;
Nserror * Error = [[Nserror alloc] init];
Nsdata * Responsedata = [Nsurlconnection sendsynchronousrequest: Request returningresponse: & Urlresponse error: & Error];
Nsstring * Result = [[Nsstring alloc] initwithdata: responsedata encoding: nsutf8stringencoding];

// Returned HTTP status  
Nslog ( @" Response Code: % d " , [Urlresponse statuscode]);

// Get returned content  
If ([Urlresponse statuscode] > =   200   && [Urlresponse statuscode] <   300 )
{
Nslog ( @" Response: % @ " , Result );
Return Result;
// Execute the content you want. The code can be written here.  
}

Return   @" Return Value " ;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.