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方式请求服务器示例的更多相关文章

  1. getJSON方式请求服务器

    register.jsp <%@ page language="java" import="java.util.*" pageEncoding=" ...

  2. Android使用HttpClient以Post、Get请求服务器发送数据的方式(普通和json)

    讲这个之前,我们先来说说get和post两种请求的区别吧!!! 1. GET提交的数据会放在URL之后,以?分割URL和传输数据,参数之间以&相连,如EditPosts.jsp?name=te ...

  3. Android 使用HTTP(get和post)方式登陆服务器

    package com.wuyou.submittoserver; import android.os.Bundle; import android.support.v7.app.ActionBarA ...

  4. Android请求服务器的两种方式--post, get的区别

    android中用get和post方式向服务器提交请求_疯狂之桥_新浪博客http://blog.sina.com.cn/s/blog_a46817ff01017yxt.html Android提交数 ...

  5. IOS 请求服务器的方式

    IOS 中请求服务器的方式主要有Get 和Post . Get :[1]向服务器发索取数据的一种请求; [2]获取信息,而不是修改信息,类似数据库查询功能一样,数据不会被修改;请求的参数会跟在url后 ...

  6. android中用get和post方式向服务器提交请求

    通过get和post方式向服务器发送请求首先说一下get和post的区别get请求方式是将提交的参数拼接在url地址后面,例如http://www.baidu.com/index.jsp?num=23 ...

  7. HttpClient get和HttpClient Post请求的方式获取服务器的返回数据

    1.转自:https://blog.csdn.net/alinshen/article/details/78221567?utm_source=blogxgwz4 /*  * 演示通过HttpClie ...

  8. Android使用HttpUrlConnection请求服务器发送数据详解

    HttpUrlConnection是java内置的api,在java.net包下,那么,它请求网络同样也有get请求和post请求两种方式.最常用的Http请求无非是get和post,get请求可以获 ...

  9. android 之HttpURLConnection的post,get方式请求数据

    get方式和post方式的区别: 1.请求的URL地址不同: post:"http://xx:8081//servlet/LoginServlet" get:http://xxx: ...

随机推荐

  1. JS限制 获取动太ID,播放视频

    JS限制textarea字数 function textdown(e) {textevent = e ; ) { return; } ) { alert("大侠,我手机屏幕小,先输入这么多字 ...

  2. 转一篇分析C语言调用时栈的变化的好文

    http://blog.csdn.net/zsy2020314/article/details/9429707

  3. 编译hadoop2.4

    摘自 http://www.aboutyun.com/thread-8130-1-1.html.http://www.dataguru.cn/forum.php?mod=viewthread& ...

  4. AI 人工智能 探索 (三)

    三类子弹的设计 using UnityEngine; using System.Collections; public class AI : AssembleModel { private Hasht ...

  5. Hrbustoj 2266 Legendary Weights(辗转相除求最大公约数)

    题意:这个题目的意思是给出一些砝码,问我们能不能根据这些砝码称量出任意重量的物品,最大公约数并不难求,难的在于如何建立这个模型. 思路:根据数论的基础知识,两个数a,b的最大公约数是a*x + b*y ...

  6. c++ 中bool 的默认值

    比如在Test.h中定义变量: _isFirst; //Test.h头文件 #ifndef __TEST_H__ #define __TEST_H__ class Test{ private: boo ...

  7. spring,hibernate配置事务

    1. 新建java project 2. 引入jar 3. src下新建package:com.web.model, com.web.dao, com.web.service, bean.xml 4. ...

  8. linux export将PATH环境变量误删了的解决办法

    今天新增环境变量的时候不小心把冒号错打成了分号 export PATH=/usr/local/php5/bin;$PATH; 导致PATH变量为/usr/local/php/bin 解决办法:[ubu ...

  9. zf-关于SYS_User表type的分类

    type=1 表示管理员 type=2 表示领导 type=3 表示非领导

  10. PL/SQL developer 管理多套数据库

    PL/SQL developer 管理多套数据库,作为一个统一的接口平台,连接多套数据库. 1.. 1.类SQL PLUS窗口:File->New->Command Window,这个类似 ...