java后台向路径发送请求获得相应参数
从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后台向路径发送请求获得相应参数的更多相关文章
- Java后台防止客户端重复请求、提交表单
前言 在Web / App项目中,有一些请求或操作会对数据产生影响(比如新增.删除.修改),针对这类请求一般都需要做一些保护,以防止用户有意或无意的重复发起这样的请求导致的数据错乱. 常见处理方案 1 ...
- iOS使用NSURLSession发送POST请求,后台无法接受到请求过来的参数
iOS中发送POST请求,有时需要设置Content-Type,尤其是上传图片的时候. application/x-www-form-urlencoded: 窗体数据被编码为名称/值对.这是标准的编码 ...
- Java得到GET和POST请求URL和参数列表
一 获取URL:getRequestURL() 二 获取参数列表: 1.getQueryString() 只适用于GET,比如客户端发送http://localhost/testServlet?a=b ...
- java后台接受web前台传递的数组参数
前台发送:&warning_type[]=1,2 &warning_type=1,2 后台接收:(@RequestParam(value = "param[]") ...
- java后台接受不到vue传的参数
@RequestMapping(value = "/delBelowImg") @Transactional public R delBelowFile(@RequestParam ...
- Android HTTP实例 使用GET方法和POST方法发送请求
Android HTTP实例 使用GET方法和POST方法发送请求 Web程序:使用GET和POST方法发送请求 首先利用MyEclispe+Tomcat写好一个Web程序,实现的功能就是提交用户信息 ...
- vue--axios发送请求
首先安装:axios $ npm install axios $ cnpm install axios //taobao源 $ bower install axios 或者使用cdn: <scr ...
- vue中使用axios发送请求
我们知道,vue2.0以后,vue就不再对vue-resource进行更新,而是推荐axios,而大型项目都会使用 Vuex 来管理数据,所以这篇博客将结合两者来发送请求 1.安装axios cnpm ...
- C# Post Get 方式发送请求
httpPost 方式发送请求 不带参数 /// <summary> /// 没有参数的post请求 /// </summary> public void HttpPostNo ...
随机推荐
- Django 使用getattr() 方法获取配置文件的变量值
在django项目的开发过程中,有时需要获取配置文件里的变量值,可以通过下面这样的方式去进行获取 from django.conf import settings item = getattr(set ...
- oi之詩
§3我看到你所说的那位OIer了. §2OIERNAME? §3是的.小心.他已达到了更高的境界.他能阅读我们的思想. §2没关系.他认为我们是代码的一部分. §3我喜欢这个OIer.他做得很好.他从 ...
- jzoj5925
tj:這道題題解有錯 水法ac代碼如下: #include<bits/stdc++.h> using namespace std; typedef long long ll; ll t,n ...
- 频繁项集挖掘之apriori和fp-growth
Apriori和fp-growth是频繁项集(frequent itemset mining)挖掘中的两个经典算法,虽然都是十几年前的,但是理解这两个算法对数据挖掘和学习算法都有很大好处.在理解这两个 ...
- Github只下载某一目录的文件
比如要下载: https://github.com/xubo245/SparkLearning/tree/master/docs 将“tree/master”改成“trunk https://gith ...
- python多态和鸭子类型
多态与多态性 多态指的是一类事物有多种形态,(一个抽象类有多个子类,因而多态的概念依赖于继承). 比如:文件分为文本文件,可执行文件(在定义角度) 比如 我们按下 F1 键这个动作: 如果当前在 Fl ...
- mapreduce程序的按照key值从大到小降序排列
在近期的Hadoop的学习中,在学习mapreduce时遇到问题:让求所给数据的top10,们我们指导mapreduce中是有默认的排列机制的,是按照key的升序从大到小排列的 然而top10问题的求 ...
- LFR benchmark graphs 人工网络生成程序
人工网络生成程序,可在CSDN上免费下载 或者科学网这边也可以下载 参数 • n: number of vertices;• k: average degree;• maxk: maximum deg ...
- (转)更换镜像rootvg卷组中的硬盘
F85系统镜像盘更换实录之一:删除原有镜像操作 # cfgmgr # lsdev -Cc disk hdisk0 Available 11-09-00-8,0 16 Bit LVD SCSI Dis ...
- C/C++ -- Gui编程 -- Qt库的使用 -- 构造函数中添加组件
在构造函数中定义一个标签,设置自动换行和样式表 -----mywidget.cpp----- #include "mywidget.h" #include "ui_myw ...