public static String sendPost(String url, String param) {
String result = "";
BufferedReader in = null;
try {
URL realUrl = new URL(url);
URLConnection connection = realUrl.openConnection();
//设置网络请求时间最多为5秒;
connection.setConnectTimeout(5000);
//读取网页请求结果时间为15秒
connection.setReadTimeout(25000);
//设置网络请求时间最多为5秒;
// 设置通用的请求属性
connection.setRequestProperty("accept", "*/*");
connection.setRequestProperty("connection", "Keep-Alive");
connection.setRequestProperty("user-agent",
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
connection.setDoOutput(true);
connection.setDoInput(true);
//设置请求编码格式
connection.setRequestProperty("Accept-Charset", "GBK");
connection.setRequestProperty("contentType", "GBK");
// 获取URLConnection对象对应的输出流
//文件流编码设置
OutputStreamWriter out = new OutputStreamWriter(connection
.getOutputStream(), "GBK");
out.write(param);
// flush输出流的缓冲
out.flush();
System.out.println("进入前....");
in = new BufferedReader(new InputStreamReader(
connection.getInputStream(),"UTF-8"));
String line;
while ((line = in.readLine()) != null) {
result += line;
}
} catch (Exception e) {
e.printStackTrace();
}
// 使用finally块来关闭输入流
finally {
try {
if (in != null) {
in.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
return result;
}
//这是我的post请求方法
String url = "http://127.0.0.1:8080/LibrarySyncMdmServer/severe/getPost";
String result=sendPost(url, URLEncoder.encode(context,"UTF-8"));
//这是调用post方法传入的参数
//这是我url的方法,现在我怎么在这个方法里获取,sendPost需要传递的参数
@Controller
@RequestMapping("/severe")
public class ViewCardNoListServerController extends BaseController {
private static Logger logger = LoggerFactory.getLogger(ViewCardNoListServerController.class);
@RequestMapping("/getPost")
public String getPost(HttpServletRequest request) throws IOException{
String str1 =request.getParameter("param");
System.out.println("你已经进入方法。。。。。"+str1);
return "sucess";
}
}