/*** 向指定URL发送POST方法的请求* * @param url* 发送请求的URL* @param param* 请求参数,请求参数应该是name1=value1&name2=value2的形式。* @return URL所代表远程资源的响应* @throws Exception*/public static String sendPost(String url, String param, boolean isGBK) throws Exception {PrintWriter out = null;BufferedReader in = null;String result = "";try {URL realUrl = new URL(url);// 打开和URL之间的连接URLConnection conn = realUrl.openConnection();conn.setRequestProperty("Charset", "UTF-8");// 设置通用的请求属性conn.setRequestProperty("accept", "*/*");conn.setRequestProperty("connection", "Keep-Alive");conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");conn.setConnectTimeout(30000);conn.setReadTimeout(30000);// 发送POST请求必须设置如下两行conn.setDoOutput(true);conn.setDoInput(true);// 获取URLConnection对象对应的输出流out = new PrintWriter(conn.getOutputStream());// 发送请求参数out.print(param);// flush输出流的缓冲out.flush();// 定义BufferedReader输入流来读取URL的响应//String charSetName = "UTF-8";if (isGBK) {charSetName = "GBK";} else {charSetName = "UTF-8";}in = new BufferedReader(new InputStreamReader(conn.getInputStream(), charSetName));String line;while ((line = in.readLine()) != null) {result += line;}} catch (Exception e) {System.out.println("发送POST请求出现异常!" + e);e.printStackTrace();throw e;}// 使用finally块来关闭输出流、输入流finally {try {if (out != null) {out.close();}if (in != null) {in.close();}} catch (IOException ex) {ex.printStackTrace();}}return result;}
详细解决方案
向指定的URL发送POST请求
热度:87 发布时间:2023-10-08 20:31:25.0
相关解决方案
- $.post()+struts2解决方法
- 关于Servlet容器和doGet/Post()的疑问,该如何处理
- 求解$.post(url,{},function(){});回调函数中怎么使用判断语句
- 关于Servlet器皿和doGet/Post()的疑问
- ajax Post 一直返回的是error ,为啥
- $.post 不走回调函数,求高手
- form表单method=“post”交付为什么路径action还出现在地址栏中
- $.post params 传值有关问题 大神们看看
- jquery-1.4.2.min.js $.post()不好用解决方法
- post 提交比get提交的安全性有关问题
- post 编码转换有关问题
- MVC jQuery Post 提交有关问题
- POST 汉字,获取值不是汉字,如何转换回来呀
- HttpWebRequest post json 数据,接受应该怎么获取啊
- 毕竟什么时候用method=post
- 帮忙看一上初学者有关问题~jquery.post()
- html post 给 asp.net,只能在 Page_Load 中获取吗?能否触发 Button1_Click 事件。该如何解决
- jquery ajax $post 为什么调用不到后台方法,该怎么处理
- 安全扫描处理 发送敏感信息时,始终使用 SSL 和 POST(主体)参数,该怎么处理
- ajax post 页面返回值有关问题
- asp.net 的 form post 哪里去了?解决方法
- 上传文件过大出现次异常,"Event message: Post 大小超出允许的限制。" ,怎么解决
- [ajax小疑点]关于xmlHttp.open("POST" url, true),详细如下
- ASP.NET 怎么设置 method="post"
- ajax有关问题 在xmlhttp.open("post"url,true)中,url为一个xml文件在服务器端的,并且xmlhttp.send(null),在响应函数里处理这个按理正常返
- ◆◇◆ 关于 htm 页面向 aspx 页面 post 的有关问题! ◆◇◆
- HTTP method POST is not supported by this URL
- post http 405
- post 与get的区别
- post 给asp页面的json数据,asp里面如何获取啊