use of org.apache.http.client.RedirectHandler in project ABPlayer by winkstu.
the class HttpUtil method GetCookie.
public static Integer GetCookie(String url, String number, String pw, String select, String host) {
System.out.println("GetCookie");
int result = 4;
HttpPost httpPost = new HttpPost(hostBase + url);
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
nvps.add(new BasicNameValuePair("number", number));
nvps.add(new BasicNameValuePair("passwd", pw));
nvps.add(new BasicNameValuePair("select", select));
BasicHttpParams httpParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParams, 10000);
HttpConnectionParams.setSoTimeout(httpParams, 10000);
try {
DefaultHttpClient httpClient = new DefaultHttpClient(httpParams);
httpClient.setRedirectHandler(new RedirectHandler() {
@Override
public boolean isRedirectRequested(HttpResponse response, HttpContext context) {
return false;
}
@Override
public URI getLocationURI(HttpResponse response, HttpContext context) throws ProtocolException {
return null;
}
});
httpPost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
HttpResponse response = httpClient.execute(httpPost);
System.out.println(response.getStatusLine().getStatusCode());
if (response.getStatusLine().getStatusCode() == 200) {
return 2;
} else if (response.getStatusLine().getStatusCode() == 302) {
Header[] headers = response.getHeaders("Location");
if (headers != null && headers.length > 0) {
List<Cookie> list = httpClient.getCookieStore().getCookies();
for (Cookie c : list) {
cookieName = c.getName();
cookieValue = c.getValue();
}
System.out.println(cookieName + cookieValue);
return 3;
}
} else if (response.getStatusLine().getStatusCode() == 404) {
return -1;
}
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
use of org.apache.http.client.RedirectHandler in project ABPlayer by winkstu.
the class HttpUtil method getCookie.
public static int getCookie(String url) {
System.out.println("getCookie" + url);
HttpGet httpGet = new HttpGet(hostBase + url);
try {
DefaultHttpClient httpClient = new DefaultHttpClient();
httpClient.setRedirectHandler(new RedirectHandler() {
@Override
public boolean isRedirectRequested(HttpResponse response, HttpContext context) {
return false;
}
@Override
public URI getLocationURI(HttpResponse response, HttpContext context) throws ProtocolException {
return null;
}
});
HttpResponse response = httpClient.execute(httpGet);
System.out.println(response.getStatusLine().getStatusCode());
System.out.println(EntityUtils.toString(response.getEntity(), HTTP.UTF_8) + "add");
if (response.getStatusLine().getStatusCode() == 200) {
Header[] heads = response.getAllHeaders();
System.out.println(heads.length);
for (Header header : heads) {
System.out.println(header.getName() + " = " + header.getValue());
}
return 2;
} else if (response.getStatusLine().getStatusCode() == 302) {
Header[] headers = response.getHeaders("Location");
if (headers != null && headers.length > 0) {
System.out.println(headers[0].getValue());
return 3;
}
} else if (response.getStatusLine().getStatusCode() == 404) {
return -1;
}
} catch (Exception e) {
e.printStackTrace();
}
return 1;
}
use of org.apache.http.client.RedirectHandler in project ABPlayer by winkstu.
the class HttpUtil method httpGetCookie.
public static String httpGetCookie(String url) {
System.out.println("httpGetCookie" + url);
HttpGet httpget = new HttpGet(hostBase + url);
String strResult = "";
BasicHttpParams httpParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParams, 15000);
HttpConnectionParams.setSoTimeout(httpParams, 15000);
try {
DefaultHttpClient httpClient = new DefaultHttpClient(httpParams);
httpClient.setRedirectHandler(new RedirectHandler() {
@Override
public boolean isRedirectRequested(HttpResponse response, HttpContext context) {
return false;
}
@Override
public URI getLocationURI(HttpResponse response, HttpContext context) throws ProtocolException {
return null;
}
});
httpget.setHeader("Cookie", cookieName + "=" + cookieValue);
HttpResponse response = httpClient.execute(httpget);
if (response.getStatusLine().getStatusCode() == 200) {
strResult = EntityUtils.toString(response.getEntity(), HTTP.UTF_8);
} else if (response.getStatusLine().getStatusCode() == 302) {
// cookieʧЧ�������ض����־�������µ�¼��ȡ
strResult = "302";
} else if (response.getStatusLine().getStatusCode() == 404) {
strResult = "-1";
}
} catch (Exception e) {
e.printStackTrace();
strResult = "4";
}
return strResult;
}
use of org.apache.http.client.RedirectHandler in project ABPlayer by winkstu.
the class HttpUtil method httpPostCookie.
public static String httpPostCookie(String url, String id, String data) {
System.out.println("httpPostCookie" + url);
String result = "4";
HttpPost httpPost = new HttpPost(hostBase + url);
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
nvps.add(new BasicNameValuePair("marc_no", id));
nvps.add(new BasicNameValuePair("r_content", data));
BasicHttpParams httpParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParams, 10000);
HttpConnectionParams.setSoTimeout(httpParams, 10000);
try {
DefaultHttpClient httpClient = new DefaultHttpClient(httpParams);
httpClient.setRedirectHandler(new RedirectHandler() {
@Override
public boolean isRedirectRequested(HttpResponse response, HttpContext context) {
return false;
}
@Override
public URI getLocationURI(HttpResponse response, HttpContext context) throws ProtocolException {
return null;
}
});
httpPost.setHeader("Cookie", cookieName + "=" + cookieValue);
httpPost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
HttpResponse response = httpClient.execute(httpPost);
System.out.println(response.getStatusLine().getStatusCode());
System.out.println(EntityUtils.toString(response.getEntity(), HTTP.UTF_8) + "add");
if (response.getStatusLine().getStatusCode() == 200) {
return "2";
} else if (response.getStatusLine().getStatusCode() == 302) {
Header[] headers = response.getHeaders("Location");
if (headers != null && headers.length > 0) {
System.out.println(headers[0].getValue());
return "3";
}
} else if (response.getStatusLine().getStatusCode() == 404) {
return "-1";
}
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
Aggregations