簡単な質問ですが、どうすればhttp://www.minecraft.net/haspaid.jsp?user=somethinghereのコンテンツを取得できますか? テキスト ファイルにユーザー名のリストがあり、それらすべてを調べて、支払ったかどうかを確認したいと考えています。この Web ページの内容は true または false のいずれかになります。html はなく、「true」または「false」のみです。どうすればそのコンテンツを入手できますか? 派手なものはいらない。Java で Web ベースのものを扱ったのはこれが初めてです。
2 に答える
2
あなたは実際にJavaAPIを使用してHTTPGETを実行する方法を尋ねています。これがコードスニペットです。
URL url = new URL("http://www.minecraft.net/haspaid.jsp?user=somethinghere");
URLConnection conn = url.openConnection();
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line = null;
while ((line = reader.readLine()) != null) {
// parse your content here
}
于 2011-11-20T09:12:27.470 に答える
1
HttpClientまたはSpringRestTemplateがその役割を果たします。
春のようなものRestTemplate
:
public class Foo {
/**
* Production HTTP end point.
*/
private static final String BASE_URL = "http://www.minecraft.net/haspaid.jsp";
/**
* {@link RestTemplate} for HTTP access.
*/
@Autowire
private RestTemplate restTemplate;
/**
* Constructor.
*/
public Foo() {
this.baseUrl = BASE_URL;
}
/**
* Constructor for testing purposes.
*
* @param baseUrl HTTP end-point url to use.
* @param restTemplate {@link RestTemplate} to use (a mock probably).
*/
protected Foo(final String baseUrl, final RestTemplate restTemplate) {
this.baseUrl = baseUrl;
this.restTemplate = restTemplate;
}
/**
* Check if user has paid.
*
* @param userName Name of the user to check.
* @return true if user has paid
*/
public boolean hasPaid(final String userName) {
if (userName == null) {
return false;
}
final String result = restTemplate.getForObject(this.baseUrl +
"?user={user}", String.class, userName);
return Boolean.valueOf(result);
}
}
于 2011-11-20T09:10:29.767 に答える