Một ví dụ nhỏ:
"username=" + URLEncoder.encode(user, "UTF-8") +
"&password=" + URLEncoder.encode(pass, "UTF-8");
URL url = new URL(address);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("POST");
//urlConnection.setRequestProperty("USER-AGENT", "Mozilla/5.0");urlConnection.setRequestProperty("ACCEPT-LANGUAGE", "en-US,en;0.5");
urlConnection.setDoOutput(true);
DataOutputStream dStream = new DataOutputStream(urlConnection.getOutputStream());
dStream.writeBytes(urlParameters);
dStream.flush();
dStream.close();
InputStream is = urlConnection.getInputStream();
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
String line;
StringBuffer response = new StringBuffer();
while ((line = rd.readLine()) != null) {
response.append(line);
response.append('\r');
}
rd.close();
return response.toString();
No comments:
Post a Comment