I keep getting a "null" exception when I run this code. If I manually type the url string into my browser the php script posts "{query_result:SUCCESS}" indicating it worked, but when I try it via this code I just get the Exception e as "null". Can anyone help?
protected String signUp(String username) {
String link = "http://12.34.56.78/JSON_addUser.php";
String data = "";
BufferedReader bufferedReader;
String result = "";
String message;
try {
data += "?username=" + URLEncoder.encode(username, "UTF-8");
link += data;
URL url = new URL(link);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
bufferedReader = new BufferedReader(new InputStreamReader(con.getInputStream()));
result = bufferedReader.readLine();
return result;
} catch (Exception e) {
message = "Exception trying: " + link + " Got: " + e.getMessage() + result;
return message;
}
}
Pretty often servers block requests like this because they are automated without a "User Agent" header. You might want to behave like a "real" browser.
Also i see a content encoding issue here. The constructor from InputStreamReader
uses the default encoding and this might not be the encoding from the server--it might crash from converting the bytes into chars.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.