gpt4 book ai didi

Android - 检查请求是 GET 还是 POST

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:48:14 34 4
gpt4 key购买 nike

我需要在我的 Android 应用程序的 shouldInterceptRequest 中检查请求是 POST 还是 GET。见下面的代码:

public class CustomWebViewClient extends WebViewClient {

...

@Override
public WebResourceResponse shouldInterceptRequest(WebView view, String url) {
if ("request is POST")
Log.d("CustomWebViewClient", "request is a POST");
else if ("request is GET")
Log.d("CustomWebViewClient", "request is a GET");

...
}
}

是否可以在 WebViewClient 的扩展中确定这一点?

最佳答案

可以通过扩展 WebViewClient 来实现,但它可能涉及比您预期的更多的工作。 WebViewClient 中的回调方法由 JNI 调用,您无法调用它来获取 header 和方法,因此最好的选择是使用 JavaScript。

此解决方案基于克里斯托夫对 http://code.google.com/p/android/issues/detail?id=9122#c21 的评论

1。创建一个名为 post_interceptor.js 的文件并将其放入 res/raw

post_interceptor.js

HTMLFormElement.prototype._submit = HTMLFormElement.prototype.submit;
HTMLFormElement.prototype.submit = interceptor;

window.addEventListener('submit', function(e) {
interceptor(e);
}, true);

function interceptor(e) {
var frm = e ? e.target : this;
interceptor_onsubmit(frm);
frm._submit();
}

function interceptor_onsubmit(f) {
var jsonArr = [];
for (i = 0; i < f.elements.length; i++) {
var parName = f.elements[i].name;
var parValue = f.elements[i].value;
var parType = f.elements[i].type;

jsonArr.push({
name : parName,
value : parValue,
type : parType
});
}

window.interception.customSubmit(JSON.stringify(jsonArr),
f.attributes['method'] === undefined ? null : f.attributes['method'].nodeValue,
f.attributes['enctype'] === undefined ? null : f.attributes['enctype'].nodeValue);
}

lastXmlhttpRequestPrototypeMethod = null;
XMLHttpRequest.prototype.reallyOpen = XMLHttpRequest.prototype.open;
XMLHttpRequest.prototype.open = function(method, url, async, user, password) {
lastXmlhttpRequestPrototypeMethod = method;
this.reallyOpen(method, url, async, user, password);
};
XMLHttpRequest.prototype.reallySend = XMLHttpRequest.prototype.send;
XMLHttpRequest.prototype.send = function(body) {
window.interception.customAjax(lastXmlhttpRequestPrototypeMethod, body);
lastXmlhttpRequestPrototypeMethod = null;
this.reallySend(body);
};

2。创建一个名为 JavascriptPostIntercept 的 Java 类

根据需要更改包/类名称。

JavascriptPostIntercept.java

public class JavascriptPostIntercept {

public interface JavascriptPostInterceptInterface {
public void nextMessageIsAjaxRequest(AjaxRequestContents contents);
public void nextMessageIsFormRequest(FormRequestContents contents);
}

private static String sInterceptHeader;

private JavascriptPostInterceptInterface mClient;

public static String getInterceptHeader() {
if (sInterceptHeader == null) {
// Assuming you have your own stream to string implementation
sInterceptHeader = StringUtils.readInputStream(
Resources.getSystem().openRawResource(R.raw.post_interceptor));
}
return sInterceptHeader;
}

public static class AjaxRequestContents {
private String mMethod;
private String mBody;

public AjaxRequestContents(String method, String body) {
mMethod = method;
mBody = body;
}

public String getMethod() {
return mMethod;
}

public String getBody() {
return mBody;
}
}

public static class FormRequestContents {
private String mJson;
private String mMethod;
private String mEnctype;

public FormRequestContents(String json, String method, String enctype) {
mJson = json;
mMethod = method;
mEnctype = enctype;
}

public String getJson() {
return mJson;
}

public String getMethod() {
return mMethod;
}

public String getEnctype() {
return mEnctype;
}
}

public JavascriptPostIntercept(JavascriptPostInterceptInterface client) {
mClient = client;
}

@JavascriptInterface
public void customAjax(final String method, final String body) {
mClient.nextMessageIsAjaxRequest(new AjaxRequestContents(method, body));
}

@JavascriptInterface
public void customSubmit(String json, String method, String enctype) {
mClient.nextMessageIsFormRequest(new FormRequestContents(json, method, enctype));
}
}

3。创建您的 WebViewClient 子类

下面的代码只获取最新请求的 HTTP 方法,这看起来足以满足您的要求,但显然 AjaxRequestContents 和 FormSubmitContents 上的其他方法可以让您访问帖子正文和其他内容(如果您需要)

class MyWebViewClient extends WebViewClient implements JavascriptPostIntercept.JavascriptPostInterceptInterface {
private String mLastRequestMethod = "GET";

/// evaluate post_interceptor.js after the page is loaded
@Override
public void onPageFinished(WebView view, String url) {
view.loadUrl("javascript: " + JavascriptPostIntercept.getInterceptHeader());
}

@TargetApi(11)
@Override
public WebResourceResponse shouldInterceptRequest(WebView view, String url) {
if (mLastRequestMethod.equals("POST")) {
// do stuff here...
} else if (mLastRequestMethod.equals("GET")) {
// do other stuff here...
}
// return something here...
}

@Override
public void nextMessageIsAjaxRequest(JavascriptPostIntercept.AjaxRequestContents contents) {
mLastRequestMethod = contents.getMethod();
}

@Override
public void nextMessageIsFormRequest(JavascriptPostIntercept.FormRequestContents contents) {
mLastRequestMethod = contents.getMethod();
}
}

4。创建适当的 JS-Java 链接

MyWebViewClient webViewClient = new MyWebViewClient();
mWebView.setWebViewClient(webViewClient);
mWebView.addJavascriptInterface(new JavascriptPostIntercept(webViewClient), "interception");

关于Android - 检查请求是 GET 还是 POST,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13237347/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com