这里有新鲜出炉的 Java 并发编程示例,程序狗速度看过来!
java 是一种可以撰写跨平台应用软件的面向对象的程序设计语言,是由 Sun Microsystems 公司于 1995 年 5 月推出的 Java 程序设计语言和 Java 平台(即 JavaEE(j2ee), JavaME(j2me), JavaSE(j2se))的总称。
这篇文章主要介绍了 java 使用 HttpURLConnection 发送数据简单实例的相关资料, 需要的朋友可以参考下
java 使用 HttpURLConnection 发送数据简单实例
每个 HttpURLConnection 实例都可用于生成单个请求,但是其他实例可以透明地共享连接到 HTTP 服务器的基础网络。请求后在 HttpURLConnection 的 InputStream 或 OutputStream 上调用 close() 方法可以释放与此实例关联的网络资源,但对共享的持久连接没有任何影响。如果在调用 disconnect() 时持久连接空闲,则可能关闭基础套接字。JAVA 使用 HttpURLConnection 发送 POST 数据是依靠 OutputStream 流的形式发送
实现代码:
- import java.io. * ;
- import java.net. * ;
- public class PostExample {
- public static void main(String[] argv) throws Exception {
- URL url = new URL("http://www.javacourses.com/cgi-bin/names.cgi");
- HttpURLConnection connection = (HttpURLConnection) url.openConnection();
- connection.setRequestMethod("POST");
- connection.setDoOutput(true);
- PrintWriter out = new PrintWriter(connection.getOutputStream());
- // encode the message
- String name = "name=" + URLEncoder.encode("Qusay Mahmoud", "UTF-8");
- String email = "email=" + URLEncoder.encode("qmahmoud@javacourses.com", "UTF-8");
- // send the encoded message
- out.println(name + "&" + email);
- out.close();
- BufferedReader in =new BufferedReader(new InputStreamReader(connection.getInputStream()));
- String line;
- while ((line = in.readLine()) != null) {
- System.out.println(line);
- } in .close();
- }
- }
来源: http://www.phperz.com/article/17/0822/338202.html