一、步骤分为
1.通过String类型的URL地址或者是String protocol, String host, int port, String file各字符串值得到URL对象
2、通过URL对象得到HTTPURLConnection
3、通过httpURLConnection对象得到请求的返回值
二、得到URL的三种方式(推荐使用第一种)
1、public URL(String url)
2、public URL(String protocol, String host, int port, String file)//可自定义请求的端口
3、public URL(String protocol, String host, String file)//默认端口为80
三、得到URLConnection对象
URLConnection urlConnection=url.openConnection();
if(urlConnection instanceof HttpURLConnection)
httpURLConnection=(HttpURLConnection) urlConnection;
四、 通过URLConnection对象得到结果的输入流
InputStream is=urlConnection.getInputStream();
DEMO如下:
I.封装类
package url;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
public class URLUtils {
/*
* 获取URL通过URL路径
*/
public static URL getURL(String strUrl)
{
try {
URL url=new URL(strUrl);
return url;
} catch (MalformedURLException e) {
System.out.println("通过地址得到URL出现异常...");
e.printStackTrace();
return null;
}
}
/*
* 通过URL对象获取Connection链接
*/
public static URLConnection getConnectionByURL(URL url,String params)
{
HttpURLConnection httpURLConnection=null;
try {
URLConnection urlConnection=url.openConnection();
if(urlConnection instanceof HttpURLConnection)
httpURLConnection=(HttpURLConnection) urlConnection;
/*
* 设置请求的方式和一些其他的参数
*/
httpURLConnection.setDoInput(true); //需要输入
httpURLConnection.setUseCaches(false); //不允许缓存
httpURLConnection.setRequestMethod("POST"); //设置POST方式连接
//设置请求属性
httpURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
httpURLConnection.setRequestProperty("Connection", "Keep-Alive");// 维持长连接
httpURLConnection.setRequestProperty("Charset", "UTF-8");
httpURLConnection.setDoOutput(true);//允许传入参数
byte[] paramByte=params.getBytes();
//参数值不要第一个的 ? 号值
httpURLConnection.getOutputStream().write(paramByte);//传入参数的byte类型
return httpURLConnection;
} catch (IOException e) {
System.out.println("获取连接异常...");
e.printStackTrace();
return null;
}
}
/*
* 通过URLConnection 对象得到请求的返回值
*/
public static String getResponseValue(URLConnection urlConnection)
{
String result="";
try {
InputStream is=urlConnection.getInputStream();
BufferedReader in = new BufferedReader(
new InputStreamReader(is));
String current;
while((current = in.readLine()) != null)
{
result += current;
}
return result;
} catch (IOException e) {
System.out.println("请求结果读取异常...");
e.printStackTrace();
return result;
}
}
/*
* 将Map类型的参数值转为String类型
*/
public static String mapToString(Map<String,Object> param)
{
StringBuffer sb=new StringBuffer("");
if(param==null||param.size()==0)
return sb.toString();
else
{
int index=1;
Set<String> keySet=param.keySet();
Iterator<String> keys=keySet.iterator();
while(keys.hasNext())
{
String key=keys.next();
String value=(String) param.get(key);
if(index==1)
{
sb.append(key+"="+value);
}else
{
sb.append("&"+key+"="+value);
}
index+=1;
}
return sb.toString();
}
}
}
II.测试类
package url;
import java.net.URL;
import java.net.URLConnection;
import java.util.HashMap;
import java.util.Map;
public class URLMain {
public static void main(String[] args) {
String url="你需要请求的URL连接";
URL obj=URLUtils.getURL(url);
Map<String,Object> map=new HashMap<String,Object>();
//可定义多个参数值 以键值对的方式放入map中 也可使用其他的方式
map.put("websiteNo", "ffb6ccf64b");
String params=URLUtils.mapToString(map);
URLConnection urlConnection=URLUtils.getConnectionByURL(obj,params);
String result=URLUtils.getResponseValue(urlConnection);
System.out.println("请求结果: "+result);
}
}
注.若是以GET的方式请求,则可以将参数直接拼接在URL地址后面,修改下面代码
httpURLConnection.setRequestMethod("GET"); //设置GET方式连接