1、WebView怎么加载post|get请求并且传参数
//webView加载post请求并传递参数
UIWebView *webView = [[UIWebView alloc] init];
NSString *bodyShare = [NSString stringWithFormat: @"hID=%@", userID];
NSMutableURLRequest * requestShare = [[NSMutableURLRequest alloc]initWithURL:[NSURL URLWithString:self.urlStr]];
[requestShare setHTTPMethod: @"POST"];
[requestShare setHTTPBody: [bodyShare dataUsingEncoding: NSUTF8StringEncoding]];
[webView loadRequest:requestShare];
//当然与之相对应的还有get请求来传递参数的,代码如下:
UIWebView *webView = [[UIWebView alloc] init];
self.urlStr = [NSString stringWithFormat:@"%@/tokenredirect?ostype=iphone&token=%@&time=%@",kServerPrefixURL,token,time];
NSURLRequest * requestShare = [[NSURLRequest alloc]initWithURL:[NSURL URLWithString:self.urlStr]];
[webView loadRequest:requestShare];
//WebView加载url直接传值
NSURL * url = [NSURL URLWithString:[[NSString stringWithFormat:@"拼接参数=%@",@"aaa"] stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]] relativeToURL:[NSURL URLWithString:string]];//path是路径
[self.webView loadRequest:[NSURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:20.f]];
网友评论