-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWebClient.java
More file actions
31 lines (27 loc) · 975 Bytes
/
Copy pathWebClient.java
File metadata and controls
31 lines (27 loc) · 975 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package com.example;
import java.io.*;
import java.net.URL;
import java.net.URLConnection;
public class WebClient {
private WebClient()
{}
public static String GetContent(String url, String oriEncoding, String targetEncoding)throws IOException{
URL u = new URL(url);
URLConnection uc = u.openConnection();
BufferedReader in;
if(oriEncoding == null || oriEncoding.length() == 0){
in = new BufferedReader(new InputStreamReader(uc.getInputStream()));
} else {
in = new BufferedReader(new InputStreamReader(uc.getInputStream(), oriEncoding));
}
String line;
StringBuilder sb = new StringBuilder();
while((line = in.readLine()) != null) {
sb.append(line);
}
if(targetEncoding == null || targetEncoding.length() == 0) {
return sb.toString();
}
return new String(sb.toString().getBytes(), targetEncoding);
}
}