package com.abin.lee.ssh.basic.train;
import java.io.BufferedInputStream;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpResponse;
import org.apache.http.HttpVersion;
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.client.params.AllClientPNames;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.junit.Test;
public class CityToCityTest {
private static final String HTTPURL="http://localhost:7500/train/mvc/train/searchTrain2";
@Test
public void testCityToCity(){
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost =new HttpPost(HTTPURL);
httpClient.getParams().setParameter(AllClientPNames.PROTOCOL_VERSION,HttpVersion.HTTP_1_1);
httpClient.getParams().setParameter(AllClientPNames.USE_EXPECT_CONTINUE, Boolean.TRUE);
httpClient.getParams().setParameter(AllClientPNames.HTTP_CONTENT_CHARSET,HTTP.UTF_8);
httpClient.getParams().setParameter(AllClientPNames.CONN_MANAGER_TIMEOUT,1000l);
httpClient.getParams().setParameter(AllClientPNames.CONNECTION_TIMEOUT, 2000);
httpClient.getParams().setParameter(AllClientPNames.SO_TIMEOUT, 10000);
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
nvps.add(new BasicNameValuePair("terminalId", "132456"));
nvps.add(new BasicNameValuePair("startCity", "北京"));
nvps.add(new BasicNameValuePair("endCity", "上海"));
nvps.add(new BasicNameValuePair("trainNumber", "T284"));
nvps.add(new BasicNameValuePair("searchType", "0"));
HttpResponse httpResponse =null;
String result="";
try {
httpPost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
httpResponse = httpClient.execute(httpPost);
BufferedInputStream buffer=new BufferedInputStream(httpResponse.getEntity().getContent());
byte[] bytes=new byte[1024];
int line=0;
StringBuilder builder=new StringBuilder();
while((line=buffer.read(bytes))!=-1){
builder.append(new String(bytes,0,line,"UTF-8"));
}
result=builder.toString();
} catch (Exception e) {
e.printStackTrace();
}finally{
if(httpPost.isAborted()){
httpPost.abort();
}
httpClient.getConnectionManager().shutdown();
}
System.out.println("result="+result);
}
}