packagebookstore;importjava.io.IOException;importjava.net.URLEncoder;importjava.util.Calendar;importjava.util.GregorianCalendar;importjavax.servlet.ServletException;importjavax.servlet.annotation.WebServlet;importjavax.servlet.http.HttpServlet;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;import org.apache.commons.httpclient.*;importorg.apache.commons.httpclient.methods.GetMethod;importorg.apache.commons.httpclient.methods.PostMethod;/*** Servlet implementation class ShareWBAPI*/@WebServlet("/ShareWBAPI")public class ShareWBAPI extendsHttpServlet {private static final long serialVersionUID = 1L;/***@seeHttpServlet#HttpServlet()*/
publicShareWBAPI() {super();//TODO Auto-generated constructor stub
}/***@seeHttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throwsServletException, IOException {//TODO Auto-generated method stub
doPost(request,response);
}/***@seeHttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throwsServletException, IOException {//TODO Auto-generated method stub
String POST_URL="http://127.0.0.1:6080/helloworld/receiveWBAPI";//URL postUrl = new URL(POST_URL);//HttpURLConnection connection = (HttpURLConnection) postUrl//.openConnection();//connection.setDoOutput(true);//connection.setDoInput(true);//connection.setRequestMethod("POST");//connection.setUseCaches(false);//connection.setInstanceFollowRedirects(true);//connection.setRequestProperty("Content-Type",//"application/x-www-form-urlencoded");//
//connection.setRequestProperty(key, value)///**//*//* 与readContentFromPost()最大的不同,设置了块大小为5字节//*///connection.setChunkedStreamingMode(5);//connection.connect();//
///**//*//* 注意,下面的getOutputStream函数工作方式于在readContentFromPost()里面的不同//* 在readContentFromPost()里面该函数仍在准备http request,没有向服务器发送任何数据//* 而在这里由于设置了ChunkedStreamingMode,getOutputStream函数会根据connect之前的配置//* 生成http request头,先发送到服务器。//*///DataOutputStream out = new DataOutputStream(connection//.getOutputStream());//String content = "userId=11111&content=" + URLEncoder.encode("一个大肥人", "utf-8");//out.writeBytes(content);//
//out.flush();//out.close();
PostMethod postMethod= newPostMethod(POST_URL);
postMethod.addParameter("userId","1111");
postMethod.addParameter("content","主题");
HttpClient client= newHttpClient();
client.setConnectionTimeout(1000 * 60);int status=0;try{
status=client.executeMethod(postMethod);
}catch(HttpException e) {//TODO Auto-generated catch block
e.printStackTrace();
}catch(IOException e) {//TODO Auto-generated catch block
e.printStackTrace();
}if(status==HttpStatus.SC_OK) {
}else{
}
postMethod.releaseConnection();//因为Bjava,redirect所以返回302,
if ((status == HttpStatus.SC_MOVED_TEMPORARILY) ||(status == HttpStatus.SC_MOVED_PERMANENTLY) ||(status == HttpStatus.SC_SEE_OTHER) || (status ==HttpStatus.SC_TEMPORARY_REDIRECT)) {//读取新的URL地址
Header header = postMethod.getResponseHeader("location");if (header != null) {
String newuri=header.getValue();if ((newuri == null) || (newuri.equals("")))
newuri="/";
GetMethod redirect= newGetMethod(newuri);
client.executeMethod(redirect);Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler "+newuri); //跳转页面效果
System.out.println("Redirect:"+redirect.getStatusLine().toString());
redirect.releaseConnection(); }else System.out.println("Invalid redirect"); }
}
}