java传递url_Java语言后台实现调用url路径并传参

本文展示了如何在Java中使用Apache HttpClient库来调用URL并传递参数。通过创建HttpClient实例,设置HttpGet请求,并处理响应,实现了从指定URL获取数据的功能。代码中详细注释了每个步骤,便于理解。
摘要由CSDN通过智能技术生成

**首先:需要俩jar包,可以在我个人资源库里下载

package test;

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStream;

import java.io.InputStreamReader;

import org.apache.http.HttpEntity;

import org.apache.http.HttpResponse;

import org.apache.http.client.ClientProtocolException;

import org.apache.http.client.HttpClient;

import org.apache.http.client.methods.HttpGet;

import org.apache.http.impl.client.DefaultHttpClient;

public class InvokingUrl {

public static void main(String[] args) throws ClientProtocolException, IOException {

url();

}

/**

* [数据连接url]

* @auther lixingshuai

* @2016-9-6  上午11:49:42

* @throws ClientProtocolException

* @throws IOException

*/

@SuppressWarnings("deprecation")

public static boolean url() throws ClientProtocolException, IOException{

// 创建HttpClient实例

HttpClient httpclient = new DefaultHttpClient();

//http://192.168.66.156:8080/calf/data?p=fun&m=synDataFromPlatform&ip=192.168.1.234&machineName=test&type=3&role=8&isEmail=1&mStatus=1&isBusiness=1&region=0

String params ="http://192.168.66.156:8080/calf/data?p=fun&m=synDataFromPlatform&ip=192.168.1.234&machineName=test&type=3&role=8&isEmail=1&mStatus=1&isBusiness=1&region=0";

//System.out.println(params);

// 创建Get方法实例

HttpGet httpgets = new HttpGet(params);

HttpResponse response = httpclient.execute(httpgets);

HttpEntity entity = response.getEntity();

if (entity != null) {

InputStream instreams = entity.getContent();

String str = convertStreamToString(instreams);

System.out.println(str);

// Do not need the rest

httpgets.abort();

return true;

}else{

return false;

}

}

public static  String convertStreamToString(InputStream is) {

BufferedReader reader = new BufferedReader(new InputStreamReader(is));

StringBuilder sb = new StringBuilder();

String line = null;

try {

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

sb.append(line + "\n");

}

} catch (IOException e) {

e.printStackTrace();

} finally {

try {

is.close();

} catch (IOException e) {

e.printStackTrace();

}

}

return sb.toString();

}

}

21d4179b55c84377bd29a664df3a23a2.jpg

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值