java http 请求get 和post
来源:互联网 发布:js冒泡排序 从小到大 编辑:程序博客网 时间:2024/05/01 00:11
代码,走您
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 TestIp {public static void main(String[] args) {String url = "http://ip.taobao.com/service/getIpInfo.php?ip=183.37.106.202";//url = "http://bbs.csdn.net/topics/300229433";try {sendGetHttp(url);sendPostHttp(url);} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}}public static void sendGetHttp(String url) throws Exception {URL url2 = new URL(url);HttpURLConnection httpUrl = (HttpURLConnection) url2.openConnection();// 连接指定的网络资源httpUrl.connect();BufferedReader reader = new BufferedReader(new InputStreamReader(httpUrl.getInputStream()));StringBuffer stringBuffer = new StringBuffer();String lines;while ((lines = reader.readLine()) != null) {stringBuffer.append(lines);}String b = stringBuffer.toString();String newStr = new String(b.getBytes(), "gb2312");System.out.println("TestIp.sendHttp()" + newStr);newStr = new String(newStr);System.out.println("TestIp.sendHttp()" + newStr);String str = "\u4e2d\u56fd";System.out.println(str);}/** * 将url 一定要写对,一定可以的 * @param url * @throws IOException */public static void sendPostHttp(String url) throws IOException { // Post请求的url,与get不同的是不需要带参数 URL postUrl = new URL(url); // 打开连接 HttpURLConnection connection = (HttpURLConnection) postUrl.openConnection(); //打开读写属性,默认均为false connection.setDoOutput(true); // 设置请求方式,默认为GET connection.setRequestMethod("POST"); // Post 请求不能使用缓存 connection.setUseCaches(false); // URLConnection.setFollowRedirects是static 函数,作用于所有的URLConnection对象。 // connection.setFollowRedirects(true); //URLConnection.setInstanceFollowRedirects 是成员函数,仅作用于当前函数 connection.setInstanceFollowRedirects(true); // 配置连接的Content-type,配置为application/x- www-form-urlencoded的意思是正文是urlencoded编码过的form参数,下面我们可以看到我们对正文内容使用URLEncoder.encode进行编码 connection.setRequestProperty(" Content-Type ", " application/x-www-form-urlencoded "); // 连接,从postUrl.openConnection()至此的配置必须要在 connect之前完成, // 要注意的是connection.getOutputStream()会隐含的进行调用 connect(),所以这里可以省略 //connection.connect(); DataOutputStream out = new DataOutputStream(connection .getOutputStream()); //正文内容其实跟get的URL中'?'后的参数字符串一致 String content = " firstname= "+URLEncoder.encode(" 一个大肥人 ", " utf-8 "); // DataOutputStream.writeBytes将字符串中的16位的 unicode字符以8位的字符形式写道流里面 out.writeBytes(content); out.flush(); out.close(); // flush and close BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); String line; System.out.println(" ============================= "); System.out.println(" Contents of post request "); System.out.println(" ============================= "); while ((line = reader.readLine()) != null) { System.out.println(line); } System.out.println(" ============================= "); System.out.println(" Contents of post request ends "); System.out.println(" ============================= "); reader.close(); //connection.disconnect(); }}
0 0
- java http 请求get 和post
- java发送http get 和 post 请求
- Java http的get和post请求
- Java http的get和post请求
- HTTP get和post请求
- http post和get请求
- java发送HTTP请求 (get和post方法请求)
- JAVA 发送Http请求,GET、POST请求
- Http协议(get请求和post请求)
- HTTP post请求和get请求
- HTTP的GET请求和POST请求
- HTTP请求的GET和POST请求
- HTTP的GET请求和POST请求
- Java模拟HTTP的Get和Post请求
- Java模拟HTTP的Get和Post请求
- Java模拟HTTP的Get和Post请求
- 纯java版本的http get和post请求
- java 发送HTTP (get 和 post方法请求)
- mac's native Terminal VS iterm2
- 使用HTML5的十大原因
- 一个老程序员对学弟学妹的一些忠告
- validate.js的使用
- UP-CUP6410开发板烧写遇到的问题和解决方法
- java http 请求get 和post
- 世界杯的资深球迷
- Service的onStartCommand方法的疑问
- 数组平衡点问题
- aio,epoll,libevent,boost::asio解决的问题
- 走进博客的世界
- quartz2.2.1 example 09代码分析
- Web 3.0时代:网络对你无所不知
- 两种排序