NSURLRequest POST方式请求服务器示例
http://lizhuang.iteye.com/blog/1833297
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];
NSURLRequest POST方式请求服务器示例的更多相关文章
- getJSON方式请求服务器
register.jsp <%@ page language="java" import="java.util.*" pageEncoding=" ...
- Android使用HttpClient以Post、Get请求服务器发送数据的方式(普通和json)
讲这个之前,我们先来说说get和post两种请求的区别吧!!! 1. GET提交的数据会放在URL之后,以?分割URL和传输数据,参数之间以&相连,如EditPosts.jsp?name=te ...
- Android 使用HTTP(get和post)方式登陆服务器
package com.wuyou.submittoserver; import android.os.Bundle; import android.support.v7.app.ActionBarA ...
- Android请求服务器的两种方式--post, get的区别
android中用get和post方式向服务器提交请求_疯狂之桥_新浪博客http://blog.sina.com.cn/s/blog_a46817ff01017yxt.html Android提交数 ...
- IOS 请求服务器的方式
IOS 中请求服务器的方式主要有Get 和Post . Get :[1]向服务器发索取数据的一种请求; [2]获取信息,而不是修改信息,类似数据库查询功能一样,数据不会被修改;请求的参数会跟在url后 ...
- android中用get和post方式向服务器提交请求
通过get和post方式向服务器发送请求首先说一下get和post的区别get请求方式是将提交的参数拼接在url地址后面,例如http://www.baidu.com/index.jsp?num=23 ...
- HttpClient get和HttpClient Post请求的方式获取服务器的返回数据
1.转自:https://blog.csdn.net/alinshen/article/details/78221567?utm_source=blogxgwz4 /* * 演示通过HttpClie ...
- Android使用HttpUrlConnection请求服务器发送数据详解
HttpUrlConnection是java内置的api,在java.net包下,那么,它请求网络同样也有get请求和post请求两种方式.最常用的Http请求无非是get和post,get请求可以获 ...
- android 之HttpURLConnection的post,get方式请求数据
get方式和post方式的区别: 1.请求的URL地址不同: post:"http://xx:8081//servlet/LoginServlet" get:http://xxx: ...
随机推荐
- Python 3.5.1 Syntax & APIs(Continue Updating..
print(x, end=' ') instead of print(x) to escape the default line-changing-output. print(str.ljust(si ...
- sqlserver 2008 查看表描述,和表结构
sp_help sys_user sp_columns sys_user --表结构 THEN obj.name ELSE '' END AS 表名, col.colorder AS 序号 , c ...
- nagios总结
主要功能 网络服务监控(SMTP.POP3.HTTP.NNTP.ICMP.SNMP.FTP.SSH) 主机资源监控(CPU load.disk usage.system logs),也包括Window ...
- iOS 开发者应该知道的 ARM 结构
http://news.cnblogs.com/n/68903/ 我在写「NEON on iPhone 入门」的时候,曾以为读者已经比较了解 iOS设备的处理器知识.然而,看过网上的一些讨论,我才发现 ...
- HDU1162-Eddy's picture(最小生成树)
Problem Description Eddy begins to like painting pictures recently ,he is sure of himself to become ...
- POJ1182--食物链(经典并查集)并查集看不出来系列2
食物链 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 65906 Accepted: 19437 Description ...
- HDU - 4994 Revenge of Nim (取石子游戏)
Problem Description Nim is a mathematical game of strategy in which two players take turns removing ...
- windows2012 r2 提高网速方法
2012 升级到 r2 微软官方地址:(感谢Monkey威武)Datacenter Preview:简体中文:http://care.dlservice.microsoft.com/dl/downlo ...
- 无法加载shockwave flash
热心网友 360浏览器的话,浏览器——工具——选项(非Internet选项)——高级设置——FLASH, 默认使用PPAPI Flash(需要重启浏览器) 默认使用NPAPI Flash(需要重启浏览 ...
- cakephp 的事件系统(Getting to grips with CakePHP’s events system), 基于观察者模式
This article was written about CakePHP 2.x and has been untested with CakePHP 3.x CakePHP seems to g ...