从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. 如何读取maven项目中的resources

    建立一个maven web项目,project-name/src/main下面有3个目录,java.resources.webapp java中存放java源代码,package等 resources ...

  2. day103 跨域请求 与频率访问限制.

    目录 一.跨域请求 二.频率访问限制 一 .同一个域下的ajax请求访问  url文件 from django.conf.urls import url from django.contrib imp ...

  3. yum 安装指定 kernel 版本源码

    yum install "kernel-devel-uname-r == $(uname -r)"

  4. 利用DNSlog回显Weblogic(CVE-2017-10271) 漏洞执行命令结果

    作者:Armyzer0 Weblogic(CVE-2017-10271) 漏洞出来以后又是一波血雨腥风,正好我昨天测试的时候发现了一个存在这个漏洞的weblogic,但是他不回显咋办呢!让他返回执行结 ...

  5. libevent源码剖析

    libevent是一个使用C语言编写的,轻量级的开源高性能网络库,使用者很多,研究者也很多.由于代码简洁,设计思想简明巧妙,因此很适合用来学习,提升自己C语言的能力. libevent有这样显著地几个 ...

  6. spring boot 使用application.properties 进行外部配置

    application.properties大家都不陌生,我们在开发的时候,经常使用它来配置一些可以手动修改而且不用编译的变量,这样的作用在于,打成war包或者jar用于生产环境时,我们可以手动修改环 ...

  7. Flask从入门到精通之使用Flask-Migrate实现数据库迁移

    在开发程序的过程中,你会发现有时需要修改数据库模型,而且修改之后还需要更新数据库.仅当数据库表不存在时,Flask-SQLAlchemy 才会根据模型进行创建.因此,更新表的唯一方式就是先删除旧表,不 ...

  8. avalon的使用与总结

    avalon是前端MVVM框架,将所有前端代码彻底分成两部分,视图的处理通过绑定实现(angular有个更炫酷的名词叫指令),业务逻辑则集中在一个个叫VM的对象中处理.我们只要操作VM的数据,它就自然 ...

  9. Dynamic Type

    啥是 Dynamic Type 动态字体,即视力不好的用户,调整了默认字体的大小,开发者应该根据这个设置,动态改变界面的字体等,保证用户能看得清楚. 这个还是蛮重要的,视力不好的人越来越多. 用户在哪 ...

  10. (转)Python - 字符串对齐

    https://zhuanlan.zhihu.com/p/33923344-----------Python小知识:用format格式化输出字符串 版权声明:本文为博主原创文章,未经博主允许不得转载. ...