java获取url返回值_java发送URL请求获取返回值

第一种方法:

import org.apache.http.HttpEntity;

import org.apache.http.HttpResponse;

import org.apache.http.NameValuePair;

import org.apache.http.client.HttpClient;

import org.apache.http.client.entity.UrlEncodedFormEntity;

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

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

import org.apache.http.message.BasicNameValuePair;

import org.apache.http.params.CoreConnectionPNames;

import org.apache.http.util.EntityUtils;

public static String sendUrlRequest(String urlStr, String param1,String param2) throws Exception {

String tempStr = null;

HttpClient httpclient = new DefaultHttpClient();

Properties properties = new Properties();

HttpEntity entity = null;

String xmlContent = "";

try

{

//设置超时时间

httpclient.getParams().setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT,20000);

httpclient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 20000);

//封装需要传递的参数

List nvps = new ArrayList ();

nvps.add(new BasicNameValuePair("mainMemoCode", strmainMemoCode));

nvps.add(new BasicNameValuePair("recordPassWord", strrecordPassWord));

//客户端的请求方法类型

HttpPost httpPost = new HttpPost(urlStr);

httpPost.setEntity(new UrlEncodedFormEntity(nvps, "GBK"));

HttpResponse response = httpclient.execute(httpPost);

//获取服务器返回Http的Content-Type的值

tempStr=response.getHeaders("Content-Type")[0].getValue().toString();

//获取服务器返回页面的值

entity = response.getEntity();

xmlContent = EntityUtils.toString(entity);

String strmessage=null;

System.out.println(xmlContent);

System.out.println(response.getHeaders("Content-Type")[0].getValue().toString());

httpPost.abort();

}

catch (SocketTimeoutException e)

{

}

catch(Exception ex)

{

ex.printStackTrace();

}

finally{

httpclient.getConnectionManager().shutdown();

}

第二种方法:

public static String sendUrlRequest(String urlStr, String param1,String param2) throws Exception {

HttpURLConnection url_con =null;

try {

URL url = new URL(urlStr);

StringBuffer bankXmlBuffer = new StringBuffer();

//创建URL连接,提交到数据,获取返回结果

HttpURLConnection connection = (HttpURLConnection) url.openConnection();

connection.setRequestMethod("POST");

connection.setDoOutput(true);

connection.setRequestProperty("User-Agent", "directclient");

PrintWriter out = new PrintWriter(new OutputStreamWriter(connection.getOutputStream(),"GBK"));

out.println(param);

out.close();

BufferedReader in = new BufferedReader(new InputStreamReader(connection

.getInputStream(),"GBK"));

String inputLine;

while ((inputLine = in.readLine()) != null) {

bankXmlBuffer.append(inputLine);

}

in.close();

tempStr = bankXmlBuffer.toString();

}

catch(Exception e)

{

System.out.println("发送GET请求出现异常!" + e);

e.printStackTrace();

} finally {

if (url_con != null)

url_con.disconnect();

}

return tmpeStr;

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值