每个 HttpURLConnection 实例都可用于生成单个请求,但是其他实例可以透明地共享连接到 HTTP 服务器的基础网络。请求后在 HttpURLConnection 的 InputStream 或 OutputStream 上调用 close() 方法可以释放与此实例关联的网络资源,但对共享的持久连接没有任何影响。如果在调用 disconnect() 时持久连接空闲,则可能关闭基础套接字。JAVA使用HttpURLConnection发送POST数据是依靠OutputStream流的形式发送

package com.newflypig.demo;
/**
* 使用jdk自带的HttpURLConnection向URL发送POST请求并输出响应结果
* 参数使用流传递,并且硬编码为字符串"name=XXX"的格式
*/
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
public class SendPostDemo {
public static void main(String[] args) throws Exception{
String urlPath = new String("http://localhost:8080/Test1/HelloWorld");
//String urlPath = new String("http://localhost:8080/Test1/HelloWorld?name=丁丁".getBytes("UTF-8"));
String param="name="+URLEncoder.encode("丁丁","UTF-8");
//建立连接
URL url=new URL(urlPath);
HttpURLConnection httpConn=(HttpURLConnection)url.openConnection();
//设置参数
httpConn.setDoOutput(true); //需要输出
httpConn.setDoInput(true); //需要输入
httpConn.setUseCaches(false); //不允许缓存
httpConn.setRequestMethod("POST"); //设置POST方式连接
//设置请求属性
httpConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
httpConn.setRequestProperty("Connection", "Keep-Alive");// 维持长连接
httpConn.setRequestProperty("Charset", "UTF-8");
//连接,也可以不用明文connect,使用下面的httpConn.getOutputStream()会自动connect
httpConn.connect();
//建立输入流,向指向的URL传入参数
DataOutputStream dos=new DataOutputStream(httpConn.getOutputStream());
dos.writeBytes(param);
dos.flush();
dos.close();
//获得响应状态
int resultCode=httpConn.getResponseCode();
if(HttpURLConnection.HTTP_OK==resultCode){
StringBuffer sb=new StringBuffer();
String readLine=new String();
BufferedReader responseReader=new BufferedReader(new InputStreamReader(httpConn.getInputStream(),"UTF-8"));
while((readLine=responseReader.readLine())!=null){
sb.append(readLine).append("\n");
}
responseReader.close();
System.out.println(sb.toString());
}
}
}

  

HttpURLConnection发送请求的更多相关文章

  1. 利用HttpURLConnection发送请求

    HttpURLConnection: 每个 HttpURLConnection实例都可用于生成单个请求,但是其他实例可以透明地共享连接到 HTTP 服务器的基础网络.请求后在 HttpURLConne ...

  2. HttpUrlConnection发送url请求(后台springmvc)

    1.HttpURLConnection发送url请求 public class JavaRequest { private static final String BASE_URL = "h ...

  3. HttpURLConnection发送POST请求(可包含文件)

    import java.io.BufferedReader; import java.io.DataOutputStream; import java.io.File; import java.io. ...

  4. Android客户端连接服务器端,向服务器端发送请求HttpURLConnection

    在Java中想后台服务器发送请求一般都直接使用了Java的网络编程,或者使用HttpClient向后台服务器端发送HTTP请求.虽然在安卓中,所有Java的API都可以使用,而却使用其并不会出现什么问 ...

  5. Http学习之使用HttpURLConnection发送post和get请求(3)

    使用HttpURLConnection发送post和get请求 但我们常常会碰到这样一种情况: 通过HttpURLConnection来模拟模拟用户登录Web服务器,服务器使用cookie进行用户认证 ...

  6. Http学习之使用HttpURLConnection发送post和get请求(2)

    接上节Http学习之使用HttpURLConnection发送post和get请求 本节深入学习post请求. 上 节说道,post请求的OutputStream实际上不是网络流,而是写入内存,在ge ...

  7. 【JAVA】通过URLConnection/HttpURLConnection发送HTTP请求的方法(一)

    Java原生的API可用于发送HTTP请求 即java.net.URL.java.net.URLConnection,JDK自带的类: 1.通过统一资源定位器(java.net.URL)获取连接器(j ...

  8. HttpURLConnection发送GET、POST请求

    HttpURLConnection发送GET.POST请求 /** * GET请求 * * @param requestUrl 请求地址 * @return */ public String get( ...

  9. HttpURLConnection 发送PUT请求 json请求体 与服务端接收

    发送请求: public void testHttp() { String result = ""; try { URL postURL = new URL("http: ...

随机推荐

  1. C# TCP 了解

    参考:http://www.jb51.net/article/118682.htm 一: TCP 粘包原理:发送方发送若干数据给接收方时粘成一包.从接收缓冲区看,后一包的头紧接前一包的数据的尾. 发送 ...

  2. Electron 打包Mac安装包代码签名问题解决方案Could not get code signature for running application

    最近一直在做electron应用的打包,集成mac版本的自动更新时出现了问题. Error: Could not get code signature for running application ...

  3. C++——函数的调用

    调用前先声明函数: >>若函数定义在调用点之前,则无需另外声明 >>若函数定义在调用点之后,则需要在调用函数前按如下形式声明函数原型(原型声明) 类型标识符   被调用函数名( ...

  4. 关于Java中equal 和 == 的区别

    在对Java开发还不熟练的时候,往往很多人都喜欢用==去比较两个对象是否相等,有时候就会出现很奇葩的问题. 其实这类问题并不是奇葩问题,只是我们不够细心而已,在Java中“==”比较两个变量本身的值, ...

  5. iOS-键盘监听YYKeyboardManager

    如果键盘弹出覆盖了原有的试图,这种效果并不好,所以我们就要在键盘弹出的时候,监听键盘的位置来改变我们一些试图的位置,例如tableView列表等:在这里推荐一个大牛ibireme写的YYKeyboar ...

  6. BZOJ 1086: [SCOI2005]王室联邦 [树上分块]

    portal 题意: 树分成若干块大小在$[s,3s]$之间,每块有一个根(可以不在块内),所有点到根路径上的点都必须在块内 据说这是一个保证了块大小直径个数的科学分块方法,貌似只有本题有用  我错了 ...

  7. Python3 栈的实现

    这篇博客主要记录我在学习python算法时实现栈的过程,这里栈的实现只是最简单的实现,其中也包括符号匹配,前缀.中缀以及后缀表达式的实例.参考书目为: problem-solving-with-alg ...

  8. SPSS 批量添加标签

  9. LeetCode - 653. Two Sum IV - Input is a BST

    Given a Binary Search Tree and a target number, return true if there exist two elements in the BST s ...

  10. windows 下编译 OpenSSL1.0.2l 版

    1.需要的软件工具: microsoft visual studio2013(或2010以后其他版本) Perl 软件, 版本为strawberry - perl - 5.26.0.1 - 64bit ...