从java后台向一路径发送请求,获得响应的参数,put get post ,还有一个返回URL的工具类,方便代码灵活修改

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLDecoder; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import com.alibaba.fastjson.JSONException;
import com.fuyin.service.UserServiceImpl; public class HttpUrl {
private static final Logger logger = LoggerFactory.getLogger(HttpUrl.class);
/**
* 输入URL 返回一卡通的参数
* @param url
* @return
*/ public static String getruslt(String url){
String urlStr ="";
String inputLine = null;
String a="";
//url = url.replaceAll("%(?![0-9a-fA-F]{2})", "%25");
//url = url.replaceAll("\\+", "%2B");
try {
urlStr = URLDecoder.decode(url, "UTF-8");
System.out.println("请求路径"+urlStr);
URL oracle = new URL(urlStr);
URLConnection conn = oracle.openConnection();
conn.setRequestProperty("Accept-Charset", "UTF-8");
BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8"));
while((inputLine = br.readLine()) != null){
a+=inputLine; }
//b=URLDecoder.decode(a,"UTF-8");
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("返回的参数"+a);
return a; } /**
* 返回一卡通链接的公用地址
* @return
*/
public static String returnip(){
//String ip="http://**********/";//测试
String ip="**********/";//生产
return ip; }
/**
* PUT方法访问
* @param url
* @return
*/
public static String getput(String url) {
BufferedReader br = null;
StringBuilder sb = new StringBuilder();
try{
URL myUrl = new URL(url);
HttpURLConnection con = (HttpURLConnection)myUrl.openConnection();
con.setDoInput(true);
con.setDoOutput(true);
con.setRequestMethod("PUT");
con.setUseCaches(false);
con.setInstanceFollowRedirects(true);
con.setRequestProperty("Content-Type", "text/plain");
con.setRequestProperty("charset", "utf-8");
con.connect(); DataOutputStream out = new DataOutputStream(con.getOutputStream());
out.flush();
out.close(); br = new BufferedReader(new InputStreamReader(con.getInputStream()));
String aLine = null;
while((aLine = br.readLine()) != null){
sb.append(aLine);
}
con.disconnect();
}catch(Exception e){ } return sb.toString();
} /**
* 使用GET的方式登录
* @param username
* @param password
* @return 登录的状态
*/
public static String loginOfGet(String url1){
HttpURLConnection conn = null;
try {
URL url = new URL(url1);
conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET");//GET和POST必须全大写
conn.setConnectTimeout(10000);//连接的超时时间
conn.setReadTimeout(5000);//读数据的超时时间
conn.setRequestProperty("Content-type", "application/json;charset=UTF-8");
int responseCode = conn.getResponseCode();
if(responseCode==200){
//访问成功,通过流取的页面的数据信息
InputStream is = conn.getInputStream();
String status = getStringFromInputStream(is);
return status;
}else{
logger.debug("访问失败:"+responseCode);
return responseCode+"";
} } catch (Exception e) {
e.printStackTrace();
} finally{
if(conn!=null){
conn.disconnect();//释放链接
}
}
return null; }
/**
* 使用POST提交方式
* @param username
* @param password
* @return
*/
public static String loginOfPost(String URL1,String str) {
HttpURLConnection conn = null;
try {
URL url = new URL(URL1);
conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("POST");//GET和POST必须全大写
conn.setConnectTimeout(10000);//连接的超时时间
conn.setReadTimeout(5000);//读数据的超时时间
conn.setDoOutput(true);//必须设置此方法 允许输出
// conn.setRequestProperty("Content-Length", 234);//设置请求消息头 可以设置多个 //post请求的参数
//String data = "username="+username+"&password="+password;
String data = str;
OutputStream out = conn.getOutputStream();
out.write(data.getBytes());
out.flush();
out.close(); int responseCode = conn.getResponseCode();
if(responseCode==200){
//访问成功,通过流取的页面的数据信息
InputStream is = conn.getInputStream();
String status = getStringFromInputStream(is);
return status;
}else{
logger.debug( "访问失败:"+responseCode);
} } catch (Exception e) {
e.printStackTrace();
} finally{
if(conn!=null){
conn.disconnect();//释放链接
}
}
return null; }
/**
* 通过字节输入流返回一个字符串信息
* @param is
* @return
* @throws IOException
*/
private static String getStringFromInputStream(InputStream is) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len=0;
while((len=is.read(buffer))!=-1){
baos.write(buffer, 0, len);
}
is.close();
//String status = baos.toString();// 把流中的数据转换成字符串, 采用的编码是: utf-8
byte[] lens = baos.toByteArray();
String status = new String(lens,"utf-8");
baos.close();
return status;
} }

java后台向路径发送请求获得相应参数的更多相关文章

  1. Java后台防止客户端重复请求、提交表单

    前言 在Web / App项目中,有一些请求或操作会对数据产生影响(比如新增.删除.修改),针对这类请求一般都需要做一些保护,以防止用户有意或无意的重复发起这样的请求导致的数据错乱. 常见处理方案 1 ...

  2. iOS使用NSURLSession发送POST请求,后台无法接受到请求过来的参数

    iOS中发送POST请求,有时需要设置Content-Type,尤其是上传图片的时候. application/x-www-form-urlencoded: 窗体数据被编码为名称/值对.这是标准的编码 ...

  3. Java得到GET和POST请求URL和参数列表

    一 获取URL:getRequestURL() 二 获取参数列表: 1.getQueryString() 只适用于GET,比如客户端发送http://localhost/testServlet?a=b ...

  4. java后台接受web前台传递的数组参数

    前台发送:&warning_type[]=1,2 &warning_type=1,2 后台接收:(@RequestParam(value = "param[]") ...

  5. java后台接受不到vue传的参数

    @RequestMapping(value = "/delBelowImg") @Transactional public R delBelowFile(@RequestParam ...

  6. Android HTTP实例 使用GET方法和POST方法发送请求

    Android HTTP实例 使用GET方法和POST方法发送请求 Web程序:使用GET和POST方法发送请求 首先利用MyEclispe+Tomcat写好一个Web程序,实现的功能就是提交用户信息 ...

  7. vue--axios发送请求

    首先安装:axios $ npm install axios $ cnpm install axios //taobao源 $ bower install axios 或者使用cdn: <scr ...

  8. vue中使用axios发送请求

    我们知道,vue2.0以后,vue就不再对vue-resource进行更新,而是推荐axios,而大型项目都会使用 Vuex 来管理数据,所以这篇博客将结合两者来发送请求 1.安装axios cnpm ...

  9. C# Post Get 方式发送请求

    httpPost 方式发送请求 不带参数 /// <summary> /// 没有参数的post请求 /// </summary> public void HttpPostNo ...

随机推荐

  1. Flask 语音分析

    1. 安装api      百度组件 pip install baidu-aip 2.登录百度ai账号 ,建立一个账号 http://ai.baidu.com/ from aip import Aip ...

  2. 程序媛计划——SQLite初级

    数据库简介 数据库定义: 指的是以一定方式储存在一起.能为多个用户共享.具有尽可能小的冗余度.与应用程序彼此独立的数据集合.是带有相关数据的表的集合. 数据库是由行和列组成的二维表. 字段: 数据库表 ...

  3. Graph-684. Redundant Connection

    In this problem, a tree is an undirected graph that is connected and has no cycles. The given input ...

  4. module.export与export的区别?

    对于大多数node初学者而言, module.exports应该都是理解的, 但多出来一个exports获取就有些疑问了 疑问一: 既然有module.exports了为什么还要有exports? 疑 ...

  5. Canvas+Js制作动量守恒的小球碰撞

    目的:通过js实现小球碰撞并实现动量守恒 canvas我们就不多说了,有用着呢. 我们可以通过canvas画2D图形(圆.方块.三角形等等)3D图形(球体.正方体等待). 当然这只是基础的皮毛而已,c ...

  6. iOS开发--应用国际化,应用内切换语言

    1.前言 自己负责的项目需要做国际化,并且要求应用内部切换语言.这个是可以做到的,也并不难,可以直接戳Github看一下 https://github.com/leo90821/Localiztion ...

  7. iOS(Swift)-Runtime之关于页面跳转的捷径【Runtime获取当前ViewController,很常用】

    写在前面 在我们操作页面跳转时,如果当前的类不是UIViewcontroller(下面用VC表示),你会不会写一个代理,或者block给VC传递信息,然后在VC里面进行 ///假如targetVc是将 ...

  8. Andrew Ng机器学习第五章——多变量线性回归

    一.多变量线性回归的技巧之一——特征缩放 1.为什么要使用特征缩放? 特征缩放用来确保特征值在相似的范围之内. 设想这样一种情况(房价预测),两个特征值分别是房子的大小和卧室的数量.每个特征值所处的范 ...

  9. (转)Python全能自动化开发环境软件之pyenv的安装说明

    原文:http://www.magedu.com/73921.html pyenv,是一款特别好用的Python版本管理器,程序员可以建立不同的目录,在不同的目录里分别运行不同版本的Python, 并 ...

  10. Vundle,Vim 的 Bundle(转)

    长久以来,我管理 Vim 配置的方式都非常原始—— zip 打包,然后发到邮箱上.偶尔会发生忘记备份,或者配置混淆的状况,不过由于懒筋发作,竟然这个方案就这么用了两年. 终有一天,我觉得这个方法太笨了 ...