Apache HttpClientユーティリティを使用すると便利です。
は、次のMavenの依存関係を追加します。
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.2</version>
</dependency>
次は、HTTP GETメソッドを使用してURLをヒットするには、次のコードを使用します。
//create the url
String url = "http://www.google.com/search?q=httpClient";
//create the http client object
HttpClient client = HttpClientBuilder.create().build();
HttpGet request = new HttpGet(url);
//execute the request and capture the response
HttpResponse response = client.execute(request);
//get response code
System.out.println("Response Code : "
+ response.getStatusLine().getStatusCode());
//get the response body
BufferedReader rd = new BufferedReader(
new InputStreamReader(response.getEntity().getContent()));
//capture the response in string
StringBuffer result = new StringBuffer();
String line = "";
while ((line = rd.readLine()) != null) {
result.append(line);
}
付加的な利点は、URLがダウンした場合のHttpClientは、自動的にURLを打つ再試行です。また、リトライイベントハンドラを提供します。
参考:Force retry on specific http status code
あなたのURLはどのように返されますか?特定のリクエストヘッダーが必要ですか? URLに接続する前にすべてを確認してください。 –
私はjavaを初めて使っています。私のURLは、sms要求が受け入れられるウェブページ上でいくつかの確認を返します。 –