`
弄月吟风
  • 浏览: 197372 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

iPhoneHttp通信

阅读更多

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];

自己封装http请求工具类,用与淘宝:

.h文件

 

//
//  HttpUtil.h
//  TaoBaoTest
//
//  Created by 杭 鲍 on 12-5-7.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//
//http请求工具类,用来进行httpget以及httppost请求,返回值是一个String类型
#import <Foundation/Foundation.h>

@interface HttpUtil : NSObject {
    
}



-(NSString *)httpGet:(NSString *)httpUrl;//传入需要请求的url
-(NSString *)httpPost:(NSString *)httpUrl:(NSString *)postData;//传入需要请求的url以及需要post的参数值
@end

 .m文件

 

#import "HttpUtil.h"

@implementation HttpUtil

-(NSString *)httpGet:(NSString *)httpUrl{
    NSMutableURLRequest *request =[[NSMutableURLRequest alloc] init]; 
	[request setURL:[NSURL URLWithString:httpUrl]]; 
	[request setHTTPMethod:@"GET"];
   // NSString *contentType = [NSString stringWithFormat:@"text/xml"]; 
    NSString *contentType = [NSString stringWithFormat:@"application/x-www-form-urlencoded"];
	[request addValue:contentType forHTTPHeaderField: @"Content-Type"];
    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];
    return result;
}
-(NSString *)httpPost:(NSString *)httpUrl:(NSString *)postData{
    NSMutableURLRequest *request =[[NSMutableURLRequest alloc] init]; 
	[request setURL:[NSURL URLWithString:httpUrl]]; 
	[request setHTTPMethod:@"POST"];
    NSString *contentType = [NSString stringWithFormat:@"application/x-www-form-urlencoded"]; 
	[request addValue:contentType forHTTPHeaderField: @"Content-Type"];
    NSMutableData *postBody = [NSMutableData data]; 
    [postBody appendData:[[NSString stringWithFormat:postData] dataUsingEncoding:NSUTF8StringEncoding]];
    [request setHTTPBody:postBody];
    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];
    return result;
}
@end

  <!--EndFragment-->

分享到:
评论
1 楼 zxsz4085 2012-08-12  
你好,最近我在弄一个应用请求php接口,接口那边偶尔会出现一个参数都接收不到的情况。请问楼主碰到过这样的情况吗?这个会不会跟封装发送http请求的方法有问题。

相关推荐

    android/iphone/windows/linux/微信 声波通信-声波wifi配码库(2021)

    详情了解:http://blog.csdn.net/softlgh/article/details/40507623 没有积分的也可从https://pan.baidu.com/s/1co0TaQ5KAFFc-MBdrIA6MA下载 后面还会上传最近两年做过的一些声音信号处理项目:声波智能锁,baby ...

    android/iphone/windows/linux/微信 声波通信-声波wifi配码库(2020)

    性能非常强,没有运行不了的平台,而且通过内存池优化,长时间解码不再分配新内存,可7*24小时运行 可支持任何平台,常见的平台android , iphone, windows, linux, arm, mipsel, stm32都有示例。 详情了解:...

    android/iphone/windows/linux声波通信库(2018)

    可支持任何平台,常见的平台android, iphone, windows, linux, arm, mipsel, stm32都有示例。 详情了解:http://blog.csdn.net/softlgh/article/details/40507623 也可从http://pan.baidu.com/s/1mi00vhe下载 后面还...

    android/iphone/windows/linux声波通信库(2017)

    iphone, windows, linux, arm, mipsel, stm32都有示例。 详情了解:http://blog.csdn.net/softlgh/article/details/40507623 也可从http://pan.baidu.com/s/1mi00vhe下载 后面还会上传最近两年做过的一些声音信号...

    android/iphone/windows/linux声波通信库(2019)

    可支持任何平台,常见的平台android, iphone, windows, linux, arm, mipsel, stm32都有示例。 详情了解:http://blog.csdn.net/softlgh/article/details/40507623 没有积分的也可从...

    android/iphone/windows/linux声波通信-声波wifi配码库(2020)

    20米以上,通过设备传输距离可在50米以上 性能非常强,没有运行不了的平台,而且通过内存池优化,长时间解码不再分配新内存,可7*24小时运行 可支持任何平台,常见的平台android , iphone, windows, linux, arm, ...

    android/iphone/windows/linux/微信 声波通信-声波wifi配码库(2023)

    详情了解:http://blog.csdn.net/softlgh/article/details/40507623 没有积分的也可从https://pan.baidu.com/s/1co0TaQ5KAFFc-MBdrIA6MA下载 后面还会上传最近两年做过的一些声音信号处理项目:声波智能锁,baby ...

    android/iphone/windows/linux/微信 声波通信-声波wifi配码库(2022)

    详情了解:http://blog.csdn.net/softlgh/article/details/40507623 没有积分的也可从https://pan.baidu.com/s/1co0TaQ5KAFFc-MBdrIA6MA下载 后面还会上传最近两年做过的一些声音信号处理项目:声波智能锁,baby ...

    串口wifi 即插即用,不需要编程,能直接跟iphone通信

    TCP/UDP/ICMP/DHCP/DNS/HTTP 支持自动和命令两种工作模式 支持串口透明传输模式 支持 AT+控制指令集 支持多种参数配置方式: 串口/WEB 服务器/无线连接 全面支持串口透明数据传输模式,真 正实现串口的即插即用 ...

    Charles for Mac V4.2.2 破解版

    Charles Mac是一款的HTTP信息抓包工具,可以有效地获取HTTP通信信息,主要用于网页的开发和调试等,Charles Mac可以看json和xml。但是其实用Google Chrome安装插件也是可以看到漂亮的格式的,可以检查HTMl,CSS和RSS...

    iOS的HTTP开发包ASIHTTPRequest

    ASI-HTTP-Request是通过CFNetwork API访问Web服务的一个开源封装类库包,使用Objective-C编写,非常易用,可以应用于iPhone和Mac OS X应用程序。 ASIHTTPRequest使用REST格式与Web服务进行通信,既支持基本的Http...

    便携式广播级视频流媒体编码器

    AU-PME9000网络媒体编码器采用最为先进的H.264/AVC视频压缩算法和MPEG4 AAC音频压缩算法...可广泛用于应急通信、网络视频直播、手机视频直播、远程会议、校园广播、医院专家会诊等众多应用领域,是移动直播的理想设备。

    IOS ASIHttpRequest资源包

    使得与Web服务器通信变得更简单。它是用Objective-C编写的,可以在MAC OS X和iPhone应用中使用。 它适用于执行基本的HTTP请求和互动(或者说是反馈)。ASIFormDataRequest子类可以简单的实现提交数据和文件。

    Android项目源码全功能语音视频通话客户端Imsdroid.zip

    IMS/ RCS iOS客户端(iPhone,iPad和iPod Touch) 4、OpenVCS 开源视频会议服务器,用于管理多点控制单元(MCU),每个MCU可以处理多达64人参与 5、Flash2IMS 使用Adobe Flash 的SIP/ IMS网关 支持功能 1、SIP...

    基于移动终端的WIFI签到系统

    【1】【通信信息报/2012年/4月/18日/第B04版 玩转终端 安卓独占中国市场七成份额 中低端手机加速规模扩张】同时Apple 手机成本相对较高,在普通高校学生使用率较低。本系统采用开发安卓的客户端简单APP应用调用安卓...

    Android应用源码45套安卓源码合集.zip

    Android应用源码串口通信(JNI)例子.rar Android应用源码任务提醒源码.rar Android应用源码仿360手机助手首页浮动菜单.rar Android应用源码仿Iphone抖动效果Shake Icon.rar Android应用源码仿QQ分组列表修改版.rar ...

    ASIHTTPRequest-ARC:ASIHTTPRequest ARC 版本

    ASIHTTPRequest 是一个围绕 CFNetwork API的易于使用的包装器,它使与 Web 服务器通信的一些更乏味的方面变得更容易。 它是用 Objective-C 编写的,适用于 Mac OS X 和 iPhone 应用程序。 它适用于执行基本的HTTP...

    node-chat-app:使用socket.io的应用

    节点聊天应用在这个项目中,我正在使用Websocket和socket.io构建一个应用程序,以实现客户端和服务器之间的双向通信。 客户端可以是Web应用程序,iphone应用程序或Android应用程序。 我将能够实时地将事件和事件数据...

    icebox:智能冰箱 Espruino 代码

    蓝牙 LE 连接到您的 iPhone(此处不包括 iPhone 应用程序) 通过类似 REST 的调用进行 BLE 通信 振动电机在结冰时“搅动水”(不是真正有效) 这是正常冰块冻结的图表。 这是冰柜的最后一个版本的样子: 这是它...

    屋里隔离网闸 OA OA系统:宇宙盾安全OA软件

    数码星辰的宇宙盾安全OA(办公自动化)系统 数码星辰的OA软件(OA系统)是集网络安全技术、信息处理技术、移动通信技术、地理信息处理,信息采集技术于一身的可以在移动环境下使用的办公系统。数码星辰的 移动办公...

Global site tag (gtag.js) - Google Analytics