我之前用HttpClient 3.0写了个java程序,我想移植到Android上面去,不过Android的HttpClient是4.0的,于是我决定在java上用HttpClient 4.0重写,下面是我的代码:
public class MyHttpClient {
public String number;
public String code="";
public StringBuffer contentBuffer = new StringBuffer();
// 构造HttpClient的实例
HttpClient httpClient=new DefaultHttpClient();
HttpGet get;
HttpPost post;
public void getCode() throws ClientProtocolException, IOException{
get = new HttpGet("http://10.1.74.16/checkcode.asp");
//发送请求
HttpResponse response = httpClient.execute(get);
InputStream is = response.getEntity().getContent();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
//保存验证码图片
String filename="G:\\2.bmp";
File storeFile = new File(filename);
FileOutputStream output=null;
output = new FileOutputStream(storeFile);
response.getEntity().writeTo(output);
output.close();
}
public void login(String UserId,String PassWord,String CheckCode) throws ClientProtocolException, IOException{
post=new HttpPost("http://10.1.74.16/login.asp");
List <NameValuePair> nvps = new ArrayList <NameValuePair>();
//提交两个参数及值
nvps.add(new BasicNameValuePair("userId", UserId));
nvps.add(new BasicNameValuePair("pwd", PassWord));
nvps.add(new BasicNameValuePair("GetCode",CheckCode));
nvps.add(new BasicNameValuePair("radioUserType","student"));
//网页编码
UrlEncodedFormEntity params = new UrlEncodedFormEntity(nvps);
post.setEntity(params);
HttpResponse response=httpClient.execute(post);
post.abort();
post=new HttpPost("http://10.1.74.16/studentWeb/studentMain.asp");
response=httpClient.execute(post);
post.abort();
HttpEntity entity = response.getEntity();
String body = EntityUtils.toString(entity , "GBK").trim();;
System.out.println(body);
}
public void getSource(String textYear) throws ClientProtocolException, IOException{
post=new HttpPost("http://10.1.74.16/studentWeb/ViewScore/ViewTermScore.asp");
List <NameValuePair> nvps = new ArrayList <NameValuePair>();
//提交两个参数及值
nvps.add(new BasicNameValuePair("textYear","2013"));
nvps.add(new BasicNameValuePair("SelectTerm","1"));
//网页编码
UrlEncodedFormEntity params = new UrlEncodedFormEntity(nvps);
post.setEntity(params);
HttpResponse response=httpClient.execute(post);
System.out.println(response.getStatusLine());
post.abort();
System.out.println(EntityUtils.toString(response.getEntity()));
HttpEntity entity = response.getEntity();
String body = EntityUtils.toString(entity , "GBK").trim();;
System.out.println(body);
}
}
前面的login方法是用来登录的,很顺利,可以返回登录之后的页面,当我执行后面的getScore方法的时候执行System.out.println(response.getStatusLine());这一句返回的是200,OK,是正常的,但是在执行System.out.println(body); 的时候就返回错误
java.net.SocketException: socket closed
at java.net.SocketInputStream.socketRead0(Native Method)
at java.net.SocketInputStream.read(Unknown Source)
at java.net.SocketInputStream.read(Unknown Source)
at org.apache.http.impl.io.AbstractSessionInputBuffer.read(AbstractSessionInputBuffer.java:187)
at org.apache.http.impl.io.ContentLengthInputStream.read(ContentLengthInputStream.java:164)
at org.apache.http.conn.EofSensorInputStream.read(EofSensorInputStream.java:138)
at sun.nio.cs.StreamDecoder.readBytes(Unknown Source)
at sun.nio.cs.StreamDecoder.implRead(Unknown Source)
at sun.nio.cs.StreamDecoder.read(Unknown Source)
at java.io.InputStreamReader.read(Unknown Source)
at java.io.Reader.read(Unknown Source)
at org.apache.http.util.EntityUtils.toString(EntityUtils.java:199)
at org.apache.http.util.EntityUtils.toString(EntityUtils.java:221)
at MyHttpClient.getSource(MyHttpClient.java:96)
at CheckCode$Listen.actionPerformed(CheckCode.java:121)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)