Post请求
public static String httpPost(String url) {
String username = "", password = "";
String str;
HttpPost request = new HttpPost(url);
HttpClient httpClient = new DefaultHttpClient();
httpClient.getParams().setIntParameter(HttpConnectionParams.SO_TIMEOUT, 5000); // 超时设置
httpClient.getParams().setIntParameter(HttpConnectionParams.CONNECTION_TIMEOUT, 5000);// 连接超时
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("username", username));
params.add(new BasicNameValuePair("password", password));
HttpResponse response;
try {
HttpEntity entity = new UrlEncodedFormEntity(params, "UTF-8");
request.setEntity(entity);
response = httpClient.execute(request);
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
str = EntityUtils.toString(response.getEntity());
JSONObject json = new JSONObject(str);
int code = json.getInt("code");
String nickname = "";
int usertype = -1;
if (code == 1) {
nickname = json.getString("nickname");
usertype = json.getInt("usertype");
}
if ((nickname.equals(username)) && (code == 1)) {
System.out.println("登陆成功!");
CookieStore mCookieStore = ((AbstractHttpClient) httpClient).getCookieStore();
List<Cookie> cookies = mCookieStore.getCookies();
for (int i = 0; i < cookies.size(); i++) {
System.out.println("session==" + cookies.get(i).getName());
}
for (int i = 0; i < cookies.size(); i++) {
// 这里是读取Cookie['JSESSIONID']的值存在静态变量中,保证每次都是同一个值
if ("JSESSIONID".equals(cookies.get(i).getName())) {
JSESSIONID = cookies.get(i).getValue();
System.out.println("JSESSIONID=" + JSESSIONID);
} else {
System.out.println("not JSESSIONID");
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return str;
}
Get请求,这里发送的Cookies会变
public static String httpGet(String url) {
HttpClient httpClient = new DefaultHttpClient();
httpClient.getParams().setIntParameter(HttpConnectionParams.SO_TIMEOUT, 10000);
httpClient.getParams().setIntParameter(HttpConnectionParams.CONNECTION_TIMEOUT, 10000);
try {
String urlEncode = encode(url, "UTF-8");
HttpGet request = new HttpGet(urlEncode);
HttpResponse response;
if (mCookies != null) {
for (int i = 0; i < mCookies.size(); i++) {
request.addHeader("Cookie", mCookies.get(i).getName() + "=" + mCookies.get(i).getValue());
}
}
response = httpClient.execute(request);
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
HTTP_RESULT = true;
HttpEntity entity = response.getEntity();
result = EntityUtils.toString(entity, HTTP.UTF_8);
CookieStore mCookieStore = ((AbstractHttpClient) httpClient).getCookieStore();
List<Cookie> cookies = mCookieStore.getCookies();
List<Integer> mCookieIndex = new ArrayList<Integer>();
if (mCookies != null) {
for (int i = 0; i < mCookies.size(); i++) {
for (int j = 0; j < cookies.size(); j++) {
boolean cookieEq = mCookies.get(i).getName().equals(cookies.get(j).getName());
if (cookieEq) {
mCookieIndex.add(i);
}
}
}
for (int i = 0; i < mCookieIndex.size(); i++) {
int index = mCookieIndex.get(i);
mCookies.remove(index);
}
}
mCookies.addAll(cookies);
} else {
HTTP_RESULT = false;
}
} catch (Exception e) {
HTTP_RESULT = false;
mCookies.clear();
System.out.println("网络连接失败 HttpUtils!");
e.printStackTrace();
}
return result;
}
URLConnection
public static String getRequestSync(String url1) {
try {
// 模拟器测试时,请使用外网地址
URL url = new URL(url1);
URLConnection con = url.openConnection();
int responseCode = ((HttpURLConnection) con).getResponseCode();
String result = "http status code: " + responseCode + "\n";
// HttpURLConnection.HTTP_OK
if (HttpURLConnection.HTTP_OK != responseCode) {
Log.i("HTTPUtils", "connection failed");
return null;
}
InputStream is = con.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
ByteArrayBuffer bab = new ByteArrayBuffer(32);
int current = 0;
while ((current = bis.read()) != -1) {
bab.append((byte) current);
}
result = EncodingUtils.getString(bab.toByteArray(), HTTP.UTF_8);
bis.close();
is.close();
return result;
} catch (Exception e) {
Log.e("HTTPUtils", "connection exception");
return null;
}
}