Skip to content

Instantly share code, notes, and snippets.

@Lerie82
Created October 20, 2023 11:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Lerie82/352b215e673775222052fb7262fd3fb7 to your computer and use it in GitHub Desktop.
Save Lerie82/352b215e673775222052fb7262fd3fb7 to your computer and use it in GitHub Desktop.
sketchware pro sending http requests
package com.my.newproject;
import java.net.*;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.Scanner;
import android.os.StrictMode;
public class MandyWeb {
private static String req_url;
private static URL url;
public MandyWeb() {
StrictMode.ThreadPolicy tp = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(tp);
}
public String sendGet(String user_url) {
try {
URL url = new URL(user_url);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("GET");
con.setRequestProperty("User-Agent", "Mozilla/5.0 (Linux; Android 13; SM-A515F) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Mobile Safari/537.36");
int responseCode = con.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) { // success
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
return response.toString();
} else {
return "GET request did not work.";
}
} catch(IOException iex){
return iex.getMessage();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment