1、服務端html
一、主要結構:java
二、主要代碼:ios
1)web.xmlweb
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <servlet> <servlet-name>LoginServlet</servlet-name><!--lsdkalskdfjasdkfj--> <servlet-class>com.wiscom.servlet.LoginServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>LoginServlet</servlet-name> <url-pattern>/LoginServlet</url-pattern> </servlet-mapping> </web-app>
2)LoginServlet.java編程
package com.wiscom.servlet; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class LoginServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub response.setContentType("text/html;charset=utf-8"); request.setCharacterEncoding("utf-8");//設置參數解碼類型,必須和頁面中一致 String sName=request.getParameter("name"); String sPassword=request.getParameter("psw"); PrintWriter out = response.getWriter(); if(sName.equals("admin")&&sPassword.equals("admin")){ out.print(sName+",您已成功登錄!!"); }else{ out.print(sName+",用戶名密碼有誤!!"); } } public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException { this.doGet(request, response); } }
2、客戶端緩存
一、頭文件:NetCenter.h服務器
#import <Foundation/Foundation.h> @interface NetCenter : NSObject @property(nonatomic,retain) NSMutableData *receiveData; @property(nonatomic,assign)int dataPackSerialNo; - (void)httpGetSyn; - (void)httpPostSyn; - (void)httpGetNoSyn; - (void)httpPostNoSyn; @end
二、實現文件:NetCenter.m網絡
#import "NetCenter.h" @implementation NetCenter @synthesize receiveData=_receiveData; @synthesize dataPackSerialNo=_dataPackSerialNo; - (void)httpGetSyn { NSLog(@"httpGetSyn..."); /* NSString *urlString =url; NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init]; [request setURL:[NSURL URLWithString:urlString]]; [request setHTTPMethod:@"GET"]; NSHTTPURLResponse* urlResponse = nil; NSError *error = [[NSError alloc] init]; NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&error]; NSMutableString *result = [[NSMutableString alloc] initWithData:responseData encoding:NSUTF8StringEncoding]; NSLog(@"The result string is :%@",result); */ //第一步,建立URL NSURL *url = [NSURL URLWithString:@"http://10.0.0.96:8080/app_server/LoginServlet?name=admin&psw=admin"]; //第二步,經過URL建立網絡請求 NSURLRequest *request = [[NSURLRequest alloc]initWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10]; //NSURLRequest初始化方法第一個參數:請求訪問路徑,第二個參數:緩存協議,第三個參數:網絡請求超時時間(秒) /* 其中緩存協議是個枚舉類型包含: NSURLRequestUseProtocolCachePolicy(基礎策略) NSURLRequestReloadIgnoringLocalCacheData(忽略本地緩存) NSURLRequestReturnCacheDataElseLoad(首先使用緩存,若是沒有本地緩存,才從原地址下載) NSURLRequestReturnCacheDataDontLoad(使用本地緩存,從不下載,若是本地沒有緩存,則請求失敗,此策略多用於離線操做) NSURLRequestReloadIgnoringLocalAndRemoteCacheData(無視任何緩存策略,不管是本地的仍是遠程的,老是從原地址從新下載) NSURLRequestReloadRevalidatingCacheData(若是本地緩存是有效的則不下載,其餘任何狀況都從原地址從新下載) */ //第三步,鏈接服務器 NSData *received = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; NSString *str = [[NSString alloc]initWithData:received encoding:NSUTF8StringEncoding]; NSLog(@"%@",str); } - (void)httpPostSyn { NSLog(@"httpPostSyn..."); //第一步,建立URL NSURL *url = [NSURL URLWithString:@"http://10.0.0.96:8080/app_server/LoginServlet"]; //第二步,建立請求 NSString *postStr = [[NSString alloc] initWithFormat:@"name=%@&psw=%@",@"admin",@"admin"]; NSData *postData = [postStr dataUsingEncoding:NSUTF8StringEncoding]; NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10]; [request setHTTPMethod:@"POST"];//設置請求方式爲POST,默認爲GET [request setHTTPBody:postData];//設置參數 //第三步,鏈接服務器 NSData *received = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; NSString *backStr = [[NSString alloc]initWithData:received encoding:NSUTF8StringEncoding]; NSLog(@"%@",backStr); } - (void)httpGetNoSyn { NSLog(@"httpGetNoSyn..."); //第一步,建立url NSURL *url = [NSURL URLWithString:@"http://10.0.0.96:8080/app_server/LoginServlet?name=admin&psw=admin"]; //第二步,建立請求 NSURLRequest *request = [[NSURLRequest alloc]initWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10]; //第三步,鏈接服務器 NSURLConnection *connection = [[NSURLConnection alloc]initWithRequest:request delegate:self]; } - (void)httpPostNoSyn { NSLog(@"httpPostNoSyn..."); //第一步,建立url NSURL *url = [NSURL URLWithString:@"http://10.0.0.96:8080/app_server/LoginServlet"]; //第二步,建立請求 NSString *postStr = [[NSString alloc] initWithFormat:@"name=%@&psw=%@",@"admin",@"admin"]; NSData *postData = [postStr dataUsingEncoding:NSUTF8StringEncoding]; NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10]; [request setHTTPMethod:@"POST"]; [request setHTTPBody:postData]; //第三步,鏈接服務器 NSURLConnection *connection = [[NSURLConnection alloc]initWithRequest:request delegate:self]; } /*************五、異步請求的代理方法[start]****************/ //接收到服務器迴應的時候調用此方法 - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { NSHTTPURLResponse *res = (NSHTTPURLResponse *)response; NSLog(@"%@",[res allHeaderFields]); self.receiveData = [NSMutableData data];//數據存儲對象的的初始化 self.dataPackSerialNo=0; NSLog(@"收到服務器迴應。。。"); } //接收到服務器傳輸數據的時候調用,此方法根據數據大小執行若干次 -(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { NSLog(@"收到服務器傳回的數據包,數據包序號:%d",self.dataPackSerialNo); [self.receiveData appendData:data]; self.dataPackSerialNo+=1; } //數據傳完以後調用此方法 -(void)connectionDidFinishLoading:(NSURLConnection *)connection { NSLog(@"數據傳輸完成,輸出全部數據結果。。。"); NSString *receiveStr = [[NSString alloc]initWithData:self.receiveData encoding:NSUTF8StringEncoding]; NSLog(@"%@",receiveStr); } //網絡請求過程當中,出現任何錯誤(斷網,鏈接超時等)會進入此方法 -(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { NSLog(@"網絡請求出錯:%@",[error localizedDescription]); } /*************五、異步請求的代理方法[end]****************/ @end
三、調用app
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; NetCenter *netCenter=[[NetCenter alloc]init]; /*****************同步請求****************/ //[netCenter httpGetSyn]; //[netCenter httpPostSyn]; /*****************異步請求****************/ //[netCenter httpGetNoSyn]; [netCenter httpPostNoSyn]; self.window.backgroundColor = [UIColor whiteColor]; [self.window makeKeyAndVisible]; return YES; }
3、測試異步
一、同步get
二、同步post
三、異步get
四、異步post
3、擴展
一、http和https
http://www.cnblogs.com/stan0714/archive/2012/03/21/2409872.html
二、ios網絡編程理論與實例