HttpUrlConnection设置User-Agent

 
 
 
 
WebView webview;
webview = new WebView(this);  

webview.layout(0000);  

WebSettings settings = webview.getSettings(); 

String ua = settings.getUserAgentString();  //获取手机内部浏览器UA

注:必须放在activity中,否则会出现空指针异常

URL myUrl = new URL(searchURL);//参数是url
HttpURLConnection myConn = (HttpURLConnection)myUrl.openConnection();//打开连接
myConn.setRequestProperty("User-agent",ua);//为连接设置ua

猜你喜欢

转载自blog.csdn.net/qq_30555429/article/details/79540357