从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;
} }