LoginSignup
2
1

More than 5 years have passed since last update.

HttpURLConnectionのUser AgentをWebViewと同じにする

Posted at

AndroidのHttpURLConnectionのUAをセットする話。
HttpURLConnectionのUAは「System.getProperty("http.agent")」の値をセットしているので、この値を書き換える。
WebViewのUAは「new WebView(this)).getSettings().getUserAgentString()」で取得する。

これを起動時に必ず実行される箇所(ActivityのonCreate()あたり)に書くと楽です。

HogeActivity.java
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
          ・・・
        System.setProperty("http.agent", (new WebView(this)).getSettings().getUserAgentString());
          ・・・
    }
2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1