package com.ui.deployuitest1;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
public class MainActivity extends Activity implements OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button bt1 = (Button) findViewById(R.id.bt1);
bt1.setOnClickListener(this);
Button bt2 = (Button) findViewById(R.id.bt2);
bt2.setOnClickListener(this);
Button bt3 = (Button) findViewById(R.id.bt3);
bt3.setOnClickListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (v.equals(findViewById(R.id.bt1))) {
String[] ctype = new String[] { "RD_15297 CHS-CORE3.49.2 已整理",
"RD_15293 EHIS-DB1.44.1 已移交",
"RD_15293 TSV2-PASMS1.17.0 已整理" };
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, ctype);
final ListView listview1 = (ListView) findViewById(R.id.listview1);
listview1.setAdapter(adapter);
}
if (v.equals(findViewById(R.id.bt2))) {
TextView tx3 = (TextView) findViewById(R.id.text3);
String htmlcontent = "";
try {
htmlcontent = getHtmlContent("http://10.11.77.18:1239");
} catch (IOException e) {
e.printStackTrace();
}
tx3.setText(htmlcontent);
}
if (v.equals(findViewById(R.id.bt3))) {
String s = "";
try {
// s = getClientCookies();
s = login();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
TextView txt4 = (TextView) findViewById(R.id.text4);
txt4.setText(s);
}
}
public String getHtmlContent(String htmlurl) throws IOException {
URL url;
String temp;
StringBuffer sb = new StringBuffer();
try {
url = new URL(htmlurl);
BufferedReader in = new BufferedReader(new InputStreamReader(
url.openStream(), "gbk"));// 读取网页全部内容
while ((temp = in.readLine()) != null) {
sb.append(temp);
}
in.close();
} catch (MalformedURLException me) {
System.out.println("你输入的URL格式有问题!请仔细输入");
me.getMessage();
throw me;
} catch (IOException e) {
e.printStackTrace();
throw e;
}
System.out.println(sb.toString());
return sb.toString();
}
public String getClientCookies() throws Exception {
/*
* DefaultHttpClient httpclient = new DefaultHttpClient(); HttpPost
* httpPost = new HttpPost("http://10.11.77.18:1230/count"); Header[]
* myheader=httpPost.getAllHeaders(); StringBuffer sb = new
* StringBuffer(); for (int i=0;i<myheader.length;i++){
* sb.append(myheader[i]); System.out.println(myheader[i]); }
* //sb.toString();
*
* System.out.println(httpPost.getAllHeaders()); //return
* httpPost.getAllHeaders().toString(); return sb.toString();
*/
URL url = new URL("http://10.11.77.18:1239/login");
HttpURLConnection huc = (HttpURLConnection) url.openConnection();
InputStream is = huc.getInputStream();
// 取得sessionID.
String cookieVal = huc.getHeaderField("Set-Cookie");
String sessionId = "1";
if (cookieVal != null) {
sessionId = cookieVal.substring(0, cookieVal.indexOf(";"));
return sessionId + "**" + sessionId.substring(19); // android_session_id=c2f4a020b37249f0e719d3d42f2a91718d21839b;
}
return sessionId;
}
/*
* 当Android应用程序访问WEB服务器的时候,我们为了与服务器保持同一会话,也就是说当前登录用户与服务器的交互是在同一个SessionId下。
* 当我们登录成功的时候,可以通过HTTP请求获取到Cookie信息,其中包括会话的SessionId,同时也可以自己将SessionId放入
* Json中返回。Session我们可以用一个静态变量来存放,每次向服务器发送请求的时候将SessionId带过去,服务器会自动检验这个
* SessionId有没有失效。 DefaultHttpClient httpclient = new DefaultHttpClient();
* HttpPost httpPost = new HttpPost(访问地址); httpPost.setHeader("Cookie",
* "JSESSIONID=" + 我们在静态变量里存放的SessionId); HttpResponse httpResponse =
* httpclient.execute(httpPost); 这样就可以将SessionId带过去了。
*/
public String login() throws Exception {
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("http://10.11.77.18:1239/login");
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("username", "admin"));
params.add(new BasicNameValuePair("password", "admin"));
httpPost.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
httpPost.setHeader("Cookie", "android_session_id=" + getClientCookies());
// android_session_id=0c54c4752c56758cba8e8b282990e1569fabfad2
// return getHtmlContent("http://10.11.77.18:1239/login");
HttpResponse httpResponse = httpclient.execute(httpPost);
//result=httpResponse.
String result = EntityUtils.toString(httpResponse.getEntity());
return result;
// return httpResponse.getAllHeaders().toString();
}
}
java发送Cookies登陆信息
最新推荐文章于 2022-01-21 08:30:00 发布