上篇文章中写了HttpURLConnection,此使用与简单的与web交互,比如复杂的带session cookie 的,它就比较难办了。具体没研究。因为它是从URL这个中get出来的
URL realurl=new URL(url1);
HttpURLConnection conn=(HttpURLConnection) realurl.openConnection();
一个HttpURLConnection对象对应一个URL,再访问另一个URL就又是另一个对象了,所以必定不会共用session的 。。。。把??? 我猜测是这个原因。
于是就用了HttpClient,它也是有两种用法一种get 一种post, get和post主要区别在于传参数,他们都可以通过setParams(params);这个方法去设置参数或者直接在url后面加参数。是的post方式也是可以直接在url+“?”+param 的。亲身验证过,而post方法还可以在entity中设置参数这是get方法所不能的。
List<NameValuePair> params = new
ArrayList<NameValuePair>();
params.add(new BasicNameValuePair
("name", name));
params.add(new BasicNameValuePair
("pass", pass));
// 设置请求参数
post.setEntity(new UrlEncodedFormEntity(
params, HTTP.UTF_8));
// 发送POST请求
HttpResponse response = httpClient
.execute(post); //④ 所以说较大点的参数可以放到entity中去。通常用法是传文件时,将文件的信息放到url后面或者setParams()中,文本流放到entity中,
比如说
InputStream in=new FileInputStream("C:/Users/COMPRQ/Desktop/er.apk");
// OutputStream ou=new FileOutputStream("C:/Users/COMPRQ/Desktop/A.apk");
InputStreamEntity reqEntity = new InputStreamEntity(in);
httppost.setEntity(reqEntity);
reqEntity.setContentType("binary/octet-stream");
来个例子
public class HttpClientTest extends Activity
{
TextView response;
HttpClient httpClient;
Handler handler = new Handler()
{
public void handleMessage(Message msg)
{
if(msg.what == 0x123)
{
// 使用response文本框显示服务器响应
response.append(msg.obj.toString() + "\n");
}
}
};
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// 创建DefaultHttpClient对象
httpClient = new DefaultHttpClient();
response = (TextView) findViewById(R.id.response);
}
public void accessSecret(View v)
{
response.setText("");
new Thread()
{
@Override
public void run()
{
// 创建一个HttpGet对象
HttpGet get = new HttpGet(
"http://192.168.200.167:8080/foo/secret.jsp"); //①
try
{
// 发送GET请求
HttpResponse httpResponse = httpClient.execute(get);//②
HttpEntity entity = httpResponse.getEntity();
if (entity != null)
{
// 读取服务器响应
BufferedReader br = new BufferedReader(
new InputStreamReader(entity.getContent()));
String line = null;
while ((line = br.readLine()) != null)
{
Message msg = new Message();
msg.what = 0x123;
msg.obj = line;
handler.sendMessage(msg);
}
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
}.start();
}
public void showLogin(View v)
{
// 加载登录界面
final View loginDialog = getLayoutInflater().inflate(
R.layout.login, null);
// 使用对话框供用户登录系统
new AlertDialog.Builder(HttpClientTest.this)
.setTitle("登录系统")
.setView(loginDialog)
.setPositiveButton("登录",
new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog,
int which)
{
// 获取用户输入的用户名、密码
final String name = ((EditText) loginDialog
.findViewById(R.id.name)).getText()
.toString();
final String pass = ((EditText) loginDialog
.findViewById(R.id.pass)).getText()
.toString();
new Thread()
{
@Override
public void run()
{
try
{
HttpPost post = new HttpPost("http://192.168.200.167:8080/foo/login.jsp");//③
// 如果传递参数个数比较多的话可以对传递的参数进行封装
List<NameValuePair> params = new
ArrayList<NameValuePair>();
params.add(new BasicNameValuePair
("name", name));
params.add(new BasicNameValuePair
("pass", pass));
// 设置请求参数
post.setEntity(new UrlEncodedFormEntity(
params, HTTP.UTF_8));
// 发送POST请求
HttpResponse response = httpClient
.execute(post); //④
// 如果服务器成功地返回响应
if (response.getStatusLine()
.getStatusCode() == 200)
{
String msg = EntityUtils
.toString(response.getEntity());
Looper.prepare();
// 提示登录成功
Toast.makeText(HttpClientTest.this,
msg, Toast.LENGTH_SHORT).show();
Looper.loop();
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
}.start();
}
}).setNegativeButton("取消", null).show();
}
}
同一个httpClient对象维护者共同的session