java net post 请求
来源:互联网 发布:远程网络学历教育 编辑:程序博客网 时间:2024/06/03 16:28
最近在看servlet,想写个接口的调用,突然发现Java.net 的很多东西都已经忘记了,在这记录下:
1)这部分是servlet的代码,想获取post请求,传递过来的两个参数
package com.test.servlet;import java.io.IOException;import java.io.PrintWriter;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;public class TestPost extends HttpServlet {public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {response.setContentType("text/html");doPost(request,response);}public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {response.setContentType("text/html");response.setCharacterEncoding("utf-8");request.setCharacterEncoding("utf-8");String name = request.getParameter("name");String pwd = request.getParameter("pwd");PrintWriter out = response.getWriter();out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");out.println("<HTML>");out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");out.println(" <BODY>");out.println(name);out.println(pwd);out.println(" </BODY>");out.println("</HTML>");out.flush();out.close();}}
2)这个部分通过执行main方法,向 TestPost 传递参数。
package com.test.post;import java.io.BufferedReader;import java.io.DataOutputStream;import java.io.IOException;import java.io.InputStreamReader;import java.net.HttpURLConnection;import java.net.URL;import java.net.URLEncoder;public class PostServlet {public static void main(String[] args) {try {URL url = new URL("http://localhost:8080/FirstServlet/servlet/TestPost");//打开连接HttpURLConnection urlConn = (HttpURLConnection)url.openConnection();//打开读写属性,默认均为falseurlConn.setDoInput(true);urlConn.setDoOutput(true);//设置请求方式urlConn.setRequestMethod("POST");//Post 请求不能使用缓存urlConn.setUseCaches(false);// URLConnection.setFollowRedirects是static 函数,作用于所有的URLConnection对象。// connection.setFollowRedirects(true);// URLConnection.setInstanceFollowRedirects 是成员函数,仅作用于当前函数urlConn.setInstanceFollowRedirects(true);// 配置连接的Content-type,配置为application/x-www-form-urlencoded的意思是//正文是urlencoded编码过的form参数,下面我们可以看到我们对正文内容使用URLEncoder.encode进行编码urlConn.setRequestProperty("Content-Type","application/x-www-form-urlencoded");// 连接,从url.openConnection()至此的配置必须要在 connect之前完成,// 要注意的是urlConn.getOutputStream()会隐含的进行调用 connect(),所以这里可以省略urlConn.connect();DataOutputStream out = new DataOutputStream(urlConn.getOutputStream());// 正文内容其实跟get的URL中'?'后的参数字符串一致// DataOutputStream.writeBytes将字符串中的16位的 unicode字符以8位的字符形式写道流里面StringBuffer buf = new StringBuffer();buf.append("name="+URLEncoder.encode("jany", "utf-8"));buf.append("&");buf.append("pwd="+URLEncoder.encode("abcjany", "utf-8"));out.writeBytes(buf.toString());out.flush();out.close(); // flush and closeBufferedReader reader = new BufferedReader(new InputStreamReader(urlConn.getInputStream()));String line;System.out.println(" ============================= ");System.out.println(" Contents of post request ");System.out.println(" ============================= ");while ((line = reader.readLine()) != null) {System.out.println("readLine:"+line);}System.out.println(" ============================= ");System.out.println(" Contents of post request ends ");System.out.println(" ============================= ");reader.close();// urlConn.disconnect();int requestCode = urlConn.getResponseCode();String requestMethod = urlConn.getRequestMethod();System.out.println("输出:"+requestCode+"/"+requestMethod);} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}
3) requestCode 从 HTTP 响应消息获取状态码。requestMethod获取请求方法。
- java net post 请求
- .NET发送POST请求
- 设置java.net.URL请求方式post get
- android post 请求 报出异常 java.net.UnknownHostException: www.****.com
- java post请求接口
- Java模拟post请求
- java post json 请求
- java get post 请求
- java get,post 请求
- Java发送Post请求
- JAVA模拟POST请求
- java POST GET 请求
- Java模拟Post请求
- JAVA模拟post请求
- java post get 请求
- java 模拟post请求
- Java模拟post请求
- java模拟post请求
- 自我管理&时间管理
- oracle中过程调用函数
- acdream 群赛5 组合数学
- protel99技巧总结
- Visio 2007 画大括号
- java net post 请求
- IE无法显示PNG格式图片的解决
- U1002:487-3279
- http常用响应状态码
- SQLServer中的字符串截取函数使用示例
- struts1 和struts2 的区别
- SSL (转自:http://kb.cnblogs.com/page/162080/)
- schema 简单编写
- Eclispe 设置字体的大小