java http 调用 接口_[Java教程]关于http请求调用接口

[Java教程]关于http请求调用接口

0 2016-05-03 10:00:10

1.这是通过json传值进行调用

(1)public static void appadd() {

try {

//创建连接

URL url = new URL(ADD_URL);

HttpURLConnection connection = (HttpURLConnection) url

.openConnection();

connection.setDoOutput(true);

connection.setDoInput(true);

connection.setRequestMethod("POST");

connection.setUseCaches(false);

connection.setInstanceFollowRedirects(true);

connection.setRequestProperty("Content-Type",

"application/x-www-form-urlencoded");

connection.connect();

//POST请求

DataOutputStream out = new DataOutputStream(

connection.getOutputStream());

JSONObject obj = new JSONObject();

obj.element("mesConent",  java.net.URLEncoder.encode("郭郭"));

obj.element("phoneList", "15662165280");

out.writeBytes(obj.toString());

out.flush();

out.close();

//读取响应

BufferedReader reader = new BufferedReader(new InputStreamReader(

connection.getInputStream()));

String lines;

StringBuffer sb = new StringBuffer("");

while ((lines = reader.readLine()) != null) {

lines = new String(lines.getBytes(), "utf-8");

sb.append(lines);

}

System.out.println(sb);

reader.close();

// 断开连接

connection.disconnect();

} catch (MalformedURLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (UnsupportedEncodingException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

(2)是json字符窜

public static String doHttpPost(String          System.out.println("发起的数据:"+         InputStream instr  = null;

java.io.ByteArrayOutputStream out = null;

try{

URL url = new URL(URL);

URLConnection urlCon = url.openConnection();

urlCon.setDoOutput(true);

urlCon.setDoInput(true);

urlCon.setUseCaches(false);

urlCon.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

DataOutputStream printout = new DataOutputStream(urlCon.getOutputStream());

printout.writeBytes(                 printout.flush();

printout.close();

//读取响应

BufferedReader reader = new BufferedReader(new InputStreamReader(

urlCon.getInputStream()));

String lines;

StringBuffer sb = new StringBuffer("");

while ((lines = reader.readLine()) != null) {

lines = new String(lines.getBytes(), "utf-8");

sb.append(lines);

}

System.out.println(sb);

reader.close();

return sb.toString();

}catch(Exception e){

e.printStackTrace();

return "0";

}

finally {

try {

out.close();

instr.close();

}catch (Exception ex) {

return "0";

}

}

}

2  通过参数传值例如url传值 ?a=?&b=?

利用httpcliend

String url="";

HttpClient client1=new HttpClient();

client1.setConnectionTimeout(30000);

PostMethod postMethod = new PostMethod(url);

client1.getParams().setContentCharset("UTF-8");

postMethod.setParameter("businesId", "1");

postMethod.setParameter("itemId", "2");     try {

client1.executeMethod(postMethod);

JSONObject result_code=  (JSONObject) JSONObject.parse(postMethod.getResponseBodyAsString());

String result_id=result_code.getString("state");

String error_msg=result_code.getString("error");

if(result_id.equals("200")){

}

} catch (HttpException e) {

// TODO 自动生成的 catch 块

e.printStackTrace();

} catch (IOException e) {

// TODO 自动生成的 catch 块

e.printStackTrace();

}

注意的事情

就是在提交请求的时候会出现编码规范

bc91bb04e6e9c61e24c974e4440db8f2.gif

application/x-www-form-urlencoded    在发送前编码所有字符(默认)

multipart/form-data     不对字符编码。在使用包含文件上传控件的表单时,必须使用该值。

text/plain     空格转换为 "+" 加号,但不对特殊字符编码。

这些是httpclient调用最基本的包

bc91bb04e6e9c61e24c974e4440db8f2.gif

本文网址:http://www.shaoqun.com/a/218285.html

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们:admin@shaoqun.com。

http

0

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值