2013-07-24 38 views
13

Tôi đang cố gắng làm cho tiêu đề WebView của tôi trông giống như người dùng chỉ sử dụng trình duyệt thông thường và không sử dụng WebView. Từ những gì tôi có thể thu thập các tiêu đề là giống hệt nhau mà WebView cũng sẽ gửi một tiêu đề X-Requested-With có chứa tên gói ứng dụng. Có cách nào để ngăn chặn điều này không?Android: Vô hiệu hóa X-Yêu cầu-Với tiêu đề Trong WebView

+1

+1 thường x-yêu cầu-có được sử dụng để phát hiện yêu cầu ajax, loại ống thứ khi android gửi tiêu đề trên _every_ yêu cầu, ajax hay không . – virtualeyes

+1

Điều này nghe có vẻ giống như http://stackoverflow.com/questions/7610790/add-custom-headers-to-webview-resource-requests-android – jlindenbaum

+0

@KingFu - Bạn có thể loại bỏ tiêu đề X-Request-With hoàn toàn không? Nguyên nhân tất cả các câu trả lời chủ yếu là về cách Sửa đổi hoặc Chỉnh sửa tiêu đề thay vì cách xóa hoặc vô hiệu hóa tiêu đề. – Manisha

Trả lời

1

Bạn có thể làm điều đó cho API Android> 11

public class AndroidMobileAppSampleActivity extends Activity { 
Map<String, String> extraHeaders = new HashMap<String, String>(); 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    WebView mainWebView = (WebView) findViewById(R.id.mainWebView); 
    // must define X-Requested-With, if header missing, then webview will 
    //add your package name 
    extraHeaders.put("X-Requested-With", "your presentation"); 
    WebSettings webSettings = mainWebView.getSettings(); 
    webSettings.setJavaScriptEnabled(true); 
    mainWebView.setWebViewClient(new MyCustomWebViewClient()); 
    mainWebView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY); 
    mainWebView.loadUrl("http://www.somesite.com", extraHeaders); 
} 

private class MyCustomWebViewClient extends WebViewClient { 
    @Override 
    public WebResourceResponse shouldInterceptRequest(WebView view, 
     String url) { 
     // TODO Here you must overwrite request using your 
     // HttpClient Request 
     // and pass it to new WebResourceResponse 
     return new WebResourceResponse(response.ContentType, response.ContentEncoding, responseStream); 
    } 
    @Override 
    public boolean shouldOverrideUrlLoading(WebView view, String url) { 
     // Apply again your heades here 
     view.loadUrl(url, extraHeaders); 
     return true; 
    } 
} 
} 
+1

Điều này có nghĩa là chúng tôi chỉ có thể sửa đổi giá trị của tiêu đề "X-Requested-With". Có cách nào chúng ta hoàn toàn có thể xóa hoặc loại bỏ nó? – Manisha

Các vấn đề liên quan