出處:http://www.cnblogs.com/edisonfeng/p/3830224.html 一、服務端 1、主要結構: 2、主要代碼: 1)web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmln
出處:http://www.cnblogs.com/edisonfeng/p/3830224.html
一、服務端
1、主要結構:
2、主要代碼:
1)web.xml
<?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); } }
二、客戶端
1、頭文件: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
2、實現文件: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]; } /*************5、非同步請求的代理方法[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]); } /*************5、非同步請求的代理方法[end]****************/ @end
3、調用
- (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; }
三、測試
1、同步get
2、同步post
3、非同步get