博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
NSURLRequest POST方式请求服务器示例
阅读量:4685 次
发布时间:2019-06-09

本文共 2306 字,大约阅读时间需要 7 分钟。

 

1、  准备阶段 

NSString *urlString = [NSString stringWithFormat:@"http://jssb.zust.edu.cn/androidLogin.action"]; 
         NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease]; 
         [request setURL:[NSURL URLWithString:urlString]]; 
       [request setHTTPMethod:@"POST"]; 
2、设置头 
         NSString *contentType = [NSString stringWithFormat:@"text/xml"]; 
         [request addValue:contentType forHTTPHeaderField: @"Content-Type"]; 
3、数据内容体的设定 
NSMutableData *postBody = [NSMutableData data]; 
[postBody appendData:[[NSString stringWithFormat:@"id=%@&password=%@&role=%@",@"admin02",@"admin02",@"dean"] dataUsingEncoding:NSUTF8StringEncoding]]; 
[request setHTTPBody:postBody]; 
XML传送的时候: 
NSMutableData *postBody = [NSMutableData data]; 
    [postBody appendData:[[NSString stringWithFormat:@"<Request  Action=\"Login\">"] dataUsingEncoding:NSUTF8StringEncoding]]; 
    [postBody appendData:[[NSString stringWithFormat:@"<Body>"] dataUsingEncoding:NSUTF8StringEncoding]]; 
    [postBody appendData:[[NSString stringWithFormat:@"<Username>wangjun</Username>"] dataUsingEncoding:NSUTF8StringEncoding]]; 
    [postBody appendData:[[NSString stringWithFormat:@"<Password>password</Password>"] dataUsingEncoding:NSUTF8StringEncoding]]; 
    [postBody appendData:[[NSString stringWithFormat:@"<PlatformID>2</PlatformID>"] dataUsingEncoding:NSUTF8StringEncoding]]; 
    [postBody appendData:[[NSString stringWithFormat:@"<PlatformVersion>3.1.3</PlatformVersion>"] dataUsingEncoding:NSUTF8StringEncoding]]; 
    [postBody appendData:[[NSString stringWithFormat:@"<TaskViewerName>IP 1.3</TaskViewerName>"] dataUsingEncoding:NSUTF8StringEncoding]]; 
    [postBody appendData:[[NSString stringWithFormat:@"<TaskViewerVersion>3</TaskViewerVersion>"] dataUsingEncoding:NSUTF8StringEncoding]]; 
    [postBody appendData:[[NSString stringWithFormat:@"</Body>"] dataUsingEncoding:NSUTF8StringEncoding]]; 
    [postBody appendData:[[NSString stringWithFormat:@"</Request>"] dataUsingEncoding:NSUTF8StringEncoding]]; 
    //post 
    [request setHTTPBody:postBody]; 
4、请求响应 
         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];

转载于:https://www.cnblogs.com/zxykit/p/6164077.html

你可能感兴趣的文章
Spring事务
查看>>
java编程基础(三)流程控制语句
查看>>
让数据库跑的更快的7个MySQL优化建议
查看>>
jquery 取id模糊查询
查看>>
解决在vue中,自用mask模态框出来后,下层的元素依旧可以滑动的问题
查看>>
修改node节点名称
查看>>
Java 文件下载
查看>>
图论——读书笔记 (深度优先搜索)
查看>>
PAT(B) 1014 福尔摩斯的约会(Java)
查看>>
PAT甲级题解-1123. Is It a Complete AVL Tree (30)-AVL树+满二叉树
查看>>
带ifrmae的弹窗
查看>>
iOS/Android 微信及浏览器中唤起本地APP
查看>>
[Usaco2005 nov]Grazing on the Run 边跑边吃草 BZOJ1742
查看>>
Spark 各个组件关系
查看>>
测试准入准出标准
查看>>
区块链学习笔记01(基本介绍)
查看>>
EF多个表映射
查看>>
J2EE项目集成SAP的BO报表
查看>>
SpringBoot--外部配置
查看>>
项目开发总结报告(GB8567——88)
查看>>