开发者

How to post a parameters to server api using HTTP POST method in iphone

i have application in which i have a web server api .this is my api

http://192.168.0.68:91/JourneyMapperAPI?RequestType=[<EntityKey>]&Command=[GET|SET|NEW]&Token=[token]&param...n=value..n

RequestType in the query string expects an entity name requested which could be any of the database tables.

Command, in the query string should specify the operation which needs to be performed on the specified request type which could be GET SET or NEW or any other entity specific command.

For eg. i have a register form which开发者_如何转开发 allows the user to register.

RequestType for register form is register so the api request on the submit button click of register form would be

http://192.168.0.68:91/JourneyMapperAPI?RequestType=Register&Command=NEW&firstname=rocky&lastname=singh&Username=rocky14&Password=[password]&Email=[email];

How to post this request to server api using http post method with all these parameters and values in it so that the values will be saved in the sever table named register .Please help me in solving this problem.thanks


You can use the below function for posting data to web-server.

-(void)callCommentWebService:(NSString *)pstrCommentXML{

    NSString *soapMsg = 
    [NSString stringWithFormat:
    @"<?xml version=\"1.0\" encoding=\"utf-8\"?>"
    "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"
    "<soap:Body>"
    "<getImageComment xmlns=\"http://tempuri.org/\">"
    "<Commentsxml>%@</Commentsxml>"
    "</getImageComment>"
    "</soap:Body>"
    "</soap:Envelope>", pstrCommentXML
    ];


    //Create URL Request
    NSURL *url = [NSURL URLWithString: @"http://www.website.com/website/WebService.asmx?op=getImageComment"];
    NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url];

    //Populate Headers
    NSString *msgLength = [NSString stringWithFormat:@"%d", [soapMsg length]];
    [req addValue:@"text/xml; charset=utf-8"  forHTTPHeaderField:@"Content-Type"];  
    [req addValue:msgLength forHTTPHeaderField:@"Content-Length"];
    [req setHTTPMethod:@"POST"];
    [req setHTTPBody: [soapMsg dataUsingEncoding:NSUTF8StringEncoding]];

    conn = [[NSURLConnection alloc] initWithRequest:req delegate:self];
    if (conn)
    {
        webData = [[NSMutableData data] retain];
    }
}

Here, url = you server web-service path

pstrCommentXML = you XML file whose format defined for upload

Then you can use simple delegate methods for getting response from server.

-(void) connection:(NSURLConnection *) connection didReceiveResponse:(NSURLResponse *) response 
{
    [webData setLength: 0];
}

-(void) connection:(NSURLConnection *) connection didReceiveData:(NSData *) data 
{
    [webData appendData:data];
}

- (void) connection:(NSURLConnection *) connection didFailWithError:(NSError *) error 
{
    [webData release];
    [connection release];
}

-(void) connectionDidFinishLoading:(NSURLConnection *) connection 
{

}

Hope you got the point.


The easiest way IMHO would be to uses ASIHTTPRequest, or it subclass ASIFormDataRequest.
This let you easily upload text and data via POST as if it would be filling a web form — hence it name.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜