import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.Cookie;
import org.jsoup.Jsoup;
public class HttpUtils {
//List<Cookie> cookies;
static List<String> cookies; //保存获取的cookie
//post的网址
private static String PATH = "http://202.118.88.140/loginAction.do";
private static URL url;
public HttpUtils() {
// TODO Auto-generated constructor stub
}
static{
try {
url = new URL(PATH);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* @param params 填写的url的参数
* @param encode 字节编码
* @return
*/
public static String sendPostMessage(Map<String, String> params,String encode){
StringBuffer buffer = new StringBuffer();
String cookie;
try {//把请求的主体写入正文!!
if(params != null&&!params.isEmpty()){
//迭代器
for(Map.Entry<String, String> entry : params.entrySet()){
buffer.append(entry.getKey()).append("=").
append(URLEncoder.encode(entry.getValue(),encode)).
append("&");
}
}
// System.out.println(buffer.toString());
//删除最后一个字符&,多了一个;主体设置完毕
buffer.deleteCharAt(buffer.length()-1);
byte[] mydata = buffer.toString().getBytes();
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setConnectTimeout(3000);
connection.setDoInput(true);//表示从服务器获取数据
connection.setDoOutput(true);//表示向服务器写数据
//获得上传信息的字节大小以及长度
connection.setRequestMethod("POST");
//是否使用缓存
connection.setUseCaches(false);
//表示设置请求体的类型是文本类型
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.setRequestProperty("Content-Length", String.valueOf(mydata.length));
connection.connect(); //连接,不写也可以。。??有待了解
// String content = "测试测试。。。";
//获得输出流,向服务器输出数据
OutputStream outputStream = connection.getOutputStream();
outputStream.write(mydata,0,mydata.length);
// outputStream.write(content.getBytes("utf-8"));
//获得服务器响应的结果和状态码
int responseCode = connection.getResponseCode();
if(responseCode == HttpURLConnection.HTTP_OK){
// 获取返回的cookie
cookies = connection.getHeaderFields().get("Set-Cookie");
System.out.println("cookie:"+cookies);
return changeInputeStream(connection.getInputStream(),encode);
}
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "";
}
/**
* 选课方案
* @param encode
* @return
*/
public static String sendGetXuanKe(String encode){
InputStream inputStream = null;
String URL_PATH="http://202.118.88.140/xkAction.do?actionType=6";
try {
URL url = new URL(URL_PATH);
if(url != null){
try {
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
//超时时间
httpURLConnection.setConnectTimeout(3000);
//表示设置本次http请求使用GET方式
httpURLConnection.setRequestMethod("GET");
for(String cookie: cookies){
httpURLConnection.addRequestProperty("Cookie", cookie.split(";",2)[0]);
}
int responsecode = httpURLConnection.getResponseCode();
if(responsecode == HttpURLConnection.HTTP_OK){
inputStream = httpURLConnection.getInputStream();
return changeInputeStream(inputStream,encode);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "";
}
/**
* 学期课表
* @param encode
* @return
*/
public static String sendGetXueQiKeBiao(String encode){
InputStream inputStream = null;
String URL_PATH="http://202.118.88.140/xskbAction.do?actionType=1";
try {
URL url = new URL(URL_PATH);
if(url != null){
try {
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
//超时时间
httpURLConnection.setConnectTimeout(3000);
//表示设置本次http请求使用GET方式
httpURLConnection.setRequestMethod("GET");
for(String cookie: cookies){
httpURLConnection.addRequestProperty("Cookie", cookie.split(";",2)[0]);
}
int responsecode = httpURLConnection.getResponseCode();
if(responsecode == HttpURLConnection.HTTP_OK){
inputStream = httpURLConnection.getInputStream();
return changeInputeStream(inputStream,encode);//把输入流转换成String
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "";
}
/**
* 将一个输入流转换成字符串
* @param inputStream
* @param encode
* @return
*/
private static String changeInputeStream(InputStream inputStream,String encode) {
//通常叫做内存流,写在内存中的
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
byte[] data = new byte[1024];
int len = 0;
String result = "";
if(inputStream != null){
try {
while((len = inputStream.read(data))!=-1){
data.toString();
outputStream.write(data, 0, len);
}
//result是在服务器端设置的doPost函数中的
result = new String(outputStream.toByteArray(),encode);
outputStream.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return result;
}
public static void main(String[] arsg){
Map<String, String> params = new HashMap<String, String>();
params.put("mm", "******"); //学号
params.put("zjh", "222013****"); //姓名,隐私原因换成星号啦
String result = sendPostMessage(params,"gb2312");
System.out.println("result->"+result);
result = sendGetXuanKe("gb2312");
System.out.println("result->"+result);
result = sendGetXueQiKeBiao("gb2312"); //返回学期课表的网页内容
System.out.println("----------------------------------------");
System.out.println("课表result->"+result);
String str = "";
System.out.println("----------------------------------------");
//System.out.println("什么东西:"+str.equals(Jsoup.parse(" ").text())+"->"+Jsoup.parse(" ").text().toString());
ParseHtml parseHtml = new ParseHtml(result);
parseHtml.parseHtml();
}
}