2011-05-26 241 views
2

我想要更改所有用户请求以使用POST方法(而不是GET)。如果请求已经是POST请求,那么在发布数据中添加一个新参数'userId = 2382938'。如果请求是GET,则将其更改为POST并添加'userId = 2382938'。修改UIWebView请求

我知道我可以用这个拦截一个UIWebView。不知道该从哪里出发。

- (BOOL)webView:(UIWebView *)webView 
shouldStartLoadWithRequest:(NSURLRequest *)request 
navigationType:(UIWebViewNavigationType)navigationType { 

//add post parameter here 
} 
+0

您可以更具体一点,你想做什么? – PengOne 2011-05-26 19:44:09

+0

我想要更改所有用户请求以使用POST方法(而不是GET)。如果请求已经是POST请求,那么在发布数据中添加一个新参数'userId = 2382938'。如果请求是GET,则将其更改为POST并添加'userId = 2382938'。由于 – Tim 2011-05-26 19:49:40

+0

假设u必须控制在网页视图中HTML,你也可以做你想做的通过捕捉形式帖子(通过的onsubmit)的HTML内,做了一些魔法在JavaScript :) – 2011-05-26 20:10:11

回答

0

How do I insert a POST request into a UIWebView

您可以使用NSMutableURLRequest, 设置HTTP方法POST,然后使用 -loadRequest 加载到你的UIWebView。

+0

如何将其转换为MutableURLRequest? – Tim 2011-05-26 20:07:43

+0

@Tim:'[request mutableCopy]'? – 2011-05-26 23:15:42

+0

因此使用该功能在我的主要职务,并在函数内部(转换请求MutableURLRequest,更改为POST,添加新的参数,然后使用和的loadRequest返回否)?它是否正确? – Tim 2011-05-26 23:25:59

0

你可能想要做那样的事情。

(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType { 

    NSMutableURLRequest *modifiedRequest = [request mutableCopy]; 

    modifiedRequest.URL = [NSURL URLWithString:parametrisedURL]; // here you will add your desired parameters 

    modifiedRequest.HTTPMethod = @"POST"; 

    [webview loadRequest:modifiedRequest]; 

    // Do other stuff if any 

    return YES; 
}