Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class WebUtils {
- public static String get(String url) throws IOException {
- HttpURLConnection con = (HttpURLConnection) new URL(url).openConnection();
- con.setRequestMethod("GET");
- con.setRequestProperty("User-Agent", "Mozilla/5.0");
- BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
- String inputLine;
- StringBuilder response = new StringBuilder();
- while ((inputLine = in.readLine()) != null) {
- response.append(inputLine)
- .append("\n");
- }
- in.close();
- return response.toString();
- }
- public static String post(String url, Map<String, String> requestMap, String body) throws IOException {
- HttpURLConnection con = (HttpURLConnection) new URL(url).openConnection();
- con.setRequestMethod("POST");
- con.setRequestProperty("User-Agent", "Mozilla/5.0");
- if (requestMap != null)
- requestMap.forEach(con::setRequestProperty);
- con.setDoOutput(true);
- con.setDoInput(true);
- DataOutputStream wr = new DataOutputStream(con.getOutputStream());
- wr.writeBytes(body);
- wr.flush();
- wr.close();
- BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
- String inputLine;
- StringBuilder response = new StringBuilder();
- while ((inputLine = in.readLine()) != null) {
- response.append(inputLine)
- .append("\n");
- }
- in.close();
- return response.toString();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement