Java 请求webServce接口 带参数
public String getWebServiceByParams(String param){ //获取基金缴付记录
// Post请求的url,与get不同的是不需要带参数
URL postUrl = null;
try {
postUrl = new URL(""); //***这里写webService接口地址
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// 打开连接
HttpURLConnection connection = null;
try {
connection = (HttpURLConnection) postUrl.openConnection();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
// 设置是否向connection输出,因为这个是post请求,参数要放在
// http正文内,因此需要设为true
connection.setDoOutput(true);
// Read from the connection. Default is true.
connection.setDoInput(true);
// 默认是 GET方式
try {
connection.setRequestMethod("POST");
} catch (ProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// Post 请求不能使用缓存
connection.setUseCaches(false);
//设置本次连接是否自动重定向
connection.setInstanceFollowRedirects(true);
// 配置本次连接的Content-type,配置为application/x-www-form-urlencoded的
// 意思是正文是urlencoded编码过的form参数
connection.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
// 连接,从postUrl.openConnection()至此的配置必须要在connect之前完成,
// 要注意的是connection.getOutputStream会隐含的进行connect。
try {
connection.connect();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
DataOutputStream out = null;
try {
out = new DataOutputStream(connection
.getOutputStream());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// 正文,正文内容其实跟get的URL中 '? '后的参数字符串一致
String content = null;
try {
content = "createDate=" + URLEncoder.encode(param, "UTF-8"); //这里改成你的参数名字
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// DataOutputStream.writeBytes将字符串中的16位的unicode字符以8位的字符形式写到流里面
try {
out.writeBytes(content);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
//流用完记得关
try {
out.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//获取响应
BufferedReader reader = null;
try {
reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
String line = null;
// while ((line = reader.readLine()) != null){
// System.out.println("line---?"+line);
//
// }
try {
line = reader.readLine();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("line---?"+line); try {
reader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//该干的都干完了,记得把连接断了
connection.disconnect();
return line;
}
Java 请求webServce接口 带参数的更多相关文章
- Java 请求webServce接口 不带参数
最近对接了个webService的接口取数据,从网上良莠不齐的代码中找到了个方法, 具体作者已经记不住是谁了,现在把代码贴出来,希望可以帮到大家,代码如下,简单粗暴 public String get ...
- java请求url可以带参数
/** * * @param urlStr * url * @param content * 提交的参数 * @param encoding * 编码格式 * @return */ public st ...
- Java 第13章 带参数的方法
带参数的方法 无参方法有那几个组成部分? 调用无参方法的两种形式是什么? 第一种:同一个类中的方法调用 直接用方法名 show(): 第二种:不同类中的方法调用 -->对象实例化 -->对 ...
- 小白的java学习之路 “ 带参数的方法”
一.带参数的方法: 1.1 语法: <访问修饰符> 返回类型 <方法名>(<形式参数列表>) { //方法的主体 } 1.2 案例: 榨汁机 public clas ...
- java请求登录接口代码示例
前言 近期研究如何利用java代码如何获取其他系统中所需的数据,自己总结的方法如下: 1.工具类代码 /** * <pre> * 方法体说明:向远程接口发起请求,返回字符串类型结果 * @ ...
- java 请求第三方接口 GET\POST 实现方法
(1)GET方法 /** * 根据高德地图api获取位置信息 * @return * */ public static String getMapAddInfo(String httpurl) { H ...
- Java带参数的线程类ParameterizedThread——即如何给Thread传递参数
在Java中似乎没有提供带运行参数的线程实现类,在第三方类库中也没有找到.网上有大量的文章在讨论这个问题,但都没有提供很好的代码封装解决方案,这令我很吃惊.如果读者知道有官方或者第三方的实现方式,欢迎 ...
- requests接口自动化2-url里不带参数的get请求
最常用的是get,post请求,然后是put,delete,其他方法很少用 1. get请求几种方式 1.1.url里不带参数的get请求 接口请求fiddler返回内容: import reques ...
- 【笔记】Asp.Net WebApi对js POST带参数跨域请求的支持方案
先说下需求:在原来的WebApi项目中增加对js跨域的请求支持,请求方式:以POST为主,webapi路由规则根据原项目需求修改如下: public static void Register(Http ...
随机推荐
- IDEA常见错误
1. inspects a maven model for resolution problems 在添加Maven依赖的时候,报了inspects a maven model for resolut ...
- VS 2010 应用程序无法启动
其实一般遇到这种问题, 不管是debug还是release, 也不用看提示的内存地址, 首先应该想到库是否包含正确. 一个可能的错误就是32位或64位不匹配的错误. 比如环境变量设的是64位的Open ...
- 关于angularJS的一些用法
AngularJS 事件指令: ng-click/dblclick ng-mousedown/up ng-mouseenter/leave ng-mousemove/over/out ng-keydo ...
- 16级第二周寒假作业E题
Home_W的位运算4 TimeLimit:2000MS MemoryLimit:128MB 64-bit integer IO format:%I64d Problem Description 给 ...
- 【Git】git clone与git pull区别
从字面意思也可以理解,都是往下拉代码,git clone是克隆,git pull 是拉.但是,也有区别: 从远程服务器克隆一个一模一样的版本库到本地,复制的是整个版本库,叫做clone.(clone是 ...
- ps aux排序
按内存升序排列: ps aux --sort=+rss 按内存降序排列: ps aux --sort=-rss 按cpu升序排列: ps aux --sort=+%cpu 为按cpu降序排列. ps ...
- Petrozavodsk Summer Training Camp 2017 Day 9
Petrozavodsk Summer Training Camp 2017 Day 9 Problem A. Building 题目描述:给出一棵树,在树上取出一条简单路径,使得该路径的最长上升子序 ...
- asyncio的简单了解
asyncio是Python 3.4版本引入的标准库,直接内置了对异步IO的支持. asyncio的编程模型就是一个消息循环.我们从asyncio模块中直接获取一个EventLoop的引用,然后把需要 ...
- hdu 5839(三维几何)
Special Tetrahedron Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Othe ...
- 495. Teemo Attacking
In LOL world, there is a hero called Teemo and his attacking can make his enemy Ashe be in poisoned ...