Nsmutableurlrequest, passing parameters in post Mode)

Source: Internet
Author: User
Address: http://www.cnblogs.com/anmog/archive/2011/03/09/1978621.html

Nsmutableurlrequest * request = [[nsmutableurlrequest alloc] init];
Nsuserdefaults * defs = [nsuserdefaults standarduserdefaults];
Nslog (@ "load currentcookie: % @", cookie );
[Request setvalue: Cookie forhttpheaderfield: @ "cookie"];
[Request seturl: [A objectatindex: 0];
[Request sethttpmethod: @ "get"];
Nsdata * returndata = [nsurlconnection sendsynchronousrequest: Request
Returningresponse: Nil error: Nil];
[Request release];
 

I. iPhone Terminal code:
(Synchronous request)
Nsstring * post = nil;
Post = [[nsstring alloc] initwithformat: @ "message = % @", @ "Hello, world."];
Nsdata * postdata = [Post datausingencoding: nsasciistringencoding allowlossyconversion: Yes];
Nsstring * postlength = [nsstring stringwithformat: @ "% d", [postdata length];
Nsmutableurlrequest * request = [[nsmutableurlrequest alloc] init] autorelease];
[Request seturl: [nsurl urlwithstring: @ "http: // 192.168.10.220: 18080/DATa/1.jsp"];
[Request sethttpmethod: @ "Post"];
[Request setvalue: postlength forhttpheaderfield: @ "Content-Length"];
[Request setvalue: @ "application/X-WWW-form-urlencoded" forhttpheaderfield: @ "Content-Type"];
[Request sethttpbody: postdata];
// [Nsurlconnection connectionwithrequest: Request delegate: Self];

// Synchronous Request Code
// Returndata is the data returned.
Nsdata * returndata = [nsurlconnection sendsynchronousrequest: Request returningrequest: Nil error: Nil];
[Post release];

Ii. Web server code
<%
String message = request. getparameter ("message ");
System. Out. println ("message =" + message );
Out. println ("message =" + message );
%>


Post string in JSON format

Static nsstring * urlstring = @ "http: // 192.168.1.103/WebAccess/admin/tablessync. aspx ";
// Static nsstring * urlstring = @ "http://www.google.cn ";
Nsmutableurlrequest * request = [[nsmutableurlrequest alloc] initwithurl: [nsurl urlwithstring: urlstring];
Nsstring * httpbodystring = @ "A test string ";

Nsmutablearray * arraydata = [[nsmutablearray alloc] init];

Nsmutabledictionary * Dlist = [[nsmutabledictionary alloc] init];
[Dlist setobject: @ "tblarea" forkey: @ "tablename"];
[Dlist setobject: @ "11:20:00" forkey: @ "lastchangedate"];
[Arraydata addobject: Dlist];

// [Request sethttpbody: [httpbodystring datausingencoding: nsutf8stringencoding];
//
// Post = [[nsstring alloc] initwithformat: @ "message = % @", @ "Hello, world."];
Httpbodystring = [arraydata jsonrepresentation];
Nsdata * postdata = [httpbodystring datausingencoding: nsasciistringencoding allowlossyconversion: Yes];
[Request sethttpbody: postdata];
[Request sethttpmethod: @ "Post"];
//
Nsurlresponse * reponse;
Nserror * error = nil;
//
Nsdata * responsedata = [nsurlconnection sendsynchronousrequest: Request returningresponse: & reponse error: & error];
If (error ){
Nslog (@ "Something Wrong: % @", [error description]);
} Else {
If (responsedata ){
Nsstring * responsestring = [[nsstring alloc] initwithdata: responsedata encoding: nsutf8stringencoding];
Nslog (@ "Get % @", responsestring );
}
}


Nsmutabledictionary * dgetdata = [responsestring jsonvalue];
Nslog (@ "dgetdata: % @", [dgetdata description]);

[Dlist release];
[Arraydata release];

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.