第四部分
4. HttpSend.java
public class Httpsend {
private static final String TAG = "Httpsend";
public static final String RESULT = "flag";
public static final String FAIL = "fail";
public static final String SUCCESS = "success";
public boolean send(String urlstr, String param) {
String result = "";
HttpURLConnection connection = null;
BufferedReader in = null;
PrintWriter out = null;
try {
URL url = new URL(urlstr);
connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("POST");
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setUseCaches(false);
connection.setInstanceFollowRedirects(true);
connection.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
connection.connect();
out = new PrintWriter(connection.getOutputStream());
out.print(param);
out.flush();
in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String s = "";
while((s = in.readLine()) != null) {
result += s;
}
} catch (IOException e) {
Log.i(TAG, "connect error");
} finally {
try {
if(out != null) {
out.close();
}
if (in != null) {
in.close();
}
} catch (IOException e) {
Log.i(TAG, "close error");
}
}
Log.i(TAG, result);
try {
JSONObject json = new JSONObject(result);
String res = (String)json.get(RESULT);
if(res.equals(SUCCESS)) {
return true;
}
}catch(JSONException e) {
Log.i(TAG, "to json error");
}
return false;
}
}