客户端代码是这样的。。。
#import "ViewController.h" @implementation ViewController -(void)aaa:(UIButton *)btn { NSString *method=[NSString stringWithFormat:@"login"]; NSString *username=[NSString stringWithFormat:@"123"]; NSString *password=[NSString stringWithFormat:@"123"]; NSString *urlString= [NSString stringWithFormat:@"http://192.168.1.107:8080/ipTest/test.do?%@%@&%@%@&%@%@",@"method=",method,@"username=",username,@"password=",password]; ASIFormDataRequest *requestForm = [[ASIFormDataRequest alloc] initWithURL:[NSURL URLWithString:urlString]]; //设置需要POST的数据,这里提交两个数据,A=a&B=b //[requestForm setPostValue:@"a" forKey:@"A"]; //[requestForm setPostValue:@"b" forKey:@"B"]; [requestForm startSynchronous]; //输入返回的信息 NSLog(@"response\n%@",[requestForm responseString]); [requestForm release]; } - (void)viewDidLoad { [super viewDidLoad]; button1=[[UIButton alloc]initWithFrame:CGRectMake(200, 200, 50, 50)]; button1.backgroundColor=[UIColor redColor]; [self.view addSubview:button1]; [button1 addTarget:self action:@selector(aaa:) forControlEvents:UIControlEventTouchUpInside]; // Do any additional setup after loading the view, typically from a nib. } - (void)viewDidUnload { [super viewDidUnload]; // Release any retained subviews of the main view. } - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown); }
客户端想要访问服务器 必须加上服务器的ip 服务端我是这样实现的。。。
服务器是用java开发的
public void doLogin(HttpServletRequest request,HttpServletResponse response) throws IOException{ String username=request.getParameter("username"); String password=request.getParameter("password"); String getStr=request.getParameter("A"); System.out.println("用户名:"+username+ "密码:"+password); getAddr(request); PrintWriter out=response.getWriter(); String msg=null; if(username!=null&&username.equals("123")&&password!=null &&password.equals("123")&& getStr!=null&&getStr.equals("a")){ msg="登陆成功"; } else { msg="登陆失败"; } out.print(msg); out.flush(); out.close(); }