public static String sendGet() {
String URL = "http://218.192.12.92/user/userinfo.aspx";
HttpClient httpClient = new DefaultHttpClient();
try {
HttpGet getMethod = new HttpGet(URL);
getMethod.setHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
getMethod.setHeader("Accept-Encoding", "gzip,deflate,sdch");
getMethod.setHeader("Accept-Language", "zh-CN,zh;q=0.8");
getMethod.setHeader("Connection", "keep-alive");
System.out.println("ASP.NET_SessionId="+cookies.get(0).getValue()+"; sulcmiswebpac="+cookies.get(1).getValue());
getMethod.setHeader("Cookie","ASP.NET_SessionId="+cookies.get(0).getValue()+"; sulcmiswebpac="+cookies.get(1).getValue());
getMethod.setHeader("Host", "218.192.12.92");
getMethod.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36");
HttpResponse response = httpClient.execute(getMethod);
int code = response.getStatusLine().getStatusCode();
Log.i(TAG, "getMethod---->"+code);
if(code == 200){
// ((AbstractHttpClient) client).getCookieStore().getCookies();
//获取httpClient的Cookies 类型为List<Cookie>
// cookies = ((AbstractHttpClient) httpClient).getCookieStore().getCookies();
// Log.i(TAG, "cookies---->"+cookies.get(0).toString());
// Log.i(TAG, "cookies---->"+cookies.get(0).getValue());
BufferedReader in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
StringBuffer sb = new StringBuffer();
String line = "";
while((line = in.readLine())!=null){
sb.append(line);
}
return sb.toString();
}
return "出错啦";
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
return "出错啦";
}
}
public static String sendPost() {
// TODO Auto-generated method stub
HttpClient httpClient = new DefaultHttpClient();
//和GET方式一样,先将参数放入List
ArrayList<NameValuePair> params = new ArrayList<NameValuePair>();
//第1
params.add(new BasicNameValuePair("__EVENTTARGET", Constants.__EVENTTARGET));
//第2
params.add(new BasicNameValuePair("__EVENTARGUMENT", Constants.__EVENTARGUMENT));
//第3
params.add(new BasicNameValuePair("__VIEWSTATE", Constants.__VIEWSTATE));
//第4
params.add(new BasicNameValuePair("__EVENTVALIDATION",Constants.__EVENTVALIDATION ));
//第5
params.add(new BasicNameValuePair("ctl00$ContentPlaceHolder1$txtlogintype", Constants.ctl00$ContentPlaceHolder1$txtlogintype));
//第6
params.add(new BasicNameValuePair("ctl00$ContentPlaceHolder1$txtUsername_Lib",Constants.ctl00$ContentPlaceHolder1$txtUsername_Lib));
//第7
params.add(new BasicNameValuePair("ctl00$ContentPlaceHolder1$txtPas_Lib",Constants.ctl00$ContentPlaceHolder1$txtPas_Lib ));
//第8
params.add(new BasicNameValuePair("ctl00$ContentPlaceHolder1$btnLogin_Lib",Constants.ctl00$ContentPlaceHolder1$btnLogin_Lib ));
try {
HttpPost postMethod = new HttpPost(Constants.WebURL);
postMethod.setEntity(new UrlEncodedFormEntity(params,HTTP.UTF_8)); //将参数填入POST Entity中 ,模拟chrome浏览器
postMethod.setHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
postMethod.setHeader("Accept-Encoding", "gzip,deflate,sdch");
postMethod.setHeader("Accept-Language", "zh-CN,zh;q=0.8");
postMethod.setHeader("Connection", "keep-alive");
postMethod.setHeader("Content-Type", "application/x-www-form-urlencoded");
postMethod.setHeader("Host", "218.192.12.92");
postMethod.setHeader("Origin", "http://218.192.12.92");
postMethod.setHeader("Referer", "http://218.192.12.92/login.aspx?ReturnUrl=%2fuser%2fuserinfo.aspx");
postMethod.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36");
HttpResponse response = httpClient.execute(postMethod); //执行POST方法
int code = response.getStatusLine().getStatusCode();
cookies = ((AbstractHttpClient) httpClient).getCookieStore().getCookies();
// System.out.println(cookies.get(0).getValue());
for(Cookie cookie:cookies){
System.out.println(cookie);
}
Log.i(TAG, ""+code);
Header[] headers = response.getAllHeaders();
for(Header header:headers){
Log.i(TAG, header.toString());
}
BufferedReader in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
StringBuffer sb = new StringBuffer();
String line = "";
while((line = in.readLine())!=null){
sb.append(line);
}
return sb.toString();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
return "chucuo";
}
}