欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

发送 GET 和 POST请求

程序员文章站 2022-04-03 08:29:48
...

/**
*@param url 发送请求的url
*@param param 请求参数 请求参数应该是 name=xx&pass=xxxx
*/
public static String sendGet(String url,String param){
String result="";
BufferedReader bfr=null;
try {
String urlName=url+"?"+param;
URL realUrl=new URL(urlName);
URLConnection conn=realUrl.openConnection();
conn.setRequestProperty("accept", "*/*");
conn.setRequestProperty("connection", "Keep-Alive");
conn.setRequestProperty("user-age", "Mozilla/4.0 (compatible;MSIE 6.0;Window NT 5.1;SV1)");

//建立实际的连接
conn.connect();
Map<String,List<String>> map=conn.getHeaderFields();
//遍历所有的响应头字段
for (String key: map.keySet()) {
System.out.println(key+"--->"+map.get(key));
}
//定义 BufferedRead输入流来读取URL的响应
bfr=new BufferedReader(new InputStreamReader(conn.getInputStream()));

String line;
while((line=bfr.readLine())!=null){
result+="\n"+line;
}
} catch (Exception e) {
System.out.print("发送GET请求出现异常:"+e);
e.printStackTrace();
}finally{
try {
if(bfr!=null)
bfr.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
return result;

}

/**
*@param url 发送请求的url
*@param param 请求参数 请求参数应该是 name=xx&pass=xxxx
*/
public static String sendPost(String url,String param){
PrintWriter out=null;
BufferedReader in=null;
String result="";
try {
URL realUrl=new URL(url);
URLConnection conn=realUrl.openConnection();
//设置通用的请求属性
conn.setRequestProperty("accept", "*/*");
conn.setRequestProperty("connection", "Keep-Alive");
conn.setRequestProperty("user-age", "Mozilla/4.0 (compatible;MSIE 6.0;Window NT 5.1;SV1)");

//发送POST请求必须设置如下两行
conn.setDoOutput(true);
conn.setDoInput(true);
out=new PrintWriter(conn.getOutputStream());
out.print(param);
out.flush();
in=new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
while((line=in.readLine())!=null){
result+="\n"+line;
}
} catch (Exception e) {
System.out.println("发送POST请求出现异常:"+e);
e.printStackTrace();
}finally{
try {
if(out!=null)
out.close();
if(in!=null)
in.close();

} catch (IOException ex) {
ex.printStackTrace();
}
}
return result;
}



Get 发送 数据

public String getJosnByUrl(String url) {
StringBuffer josn = new StringBuffer();
BufferedReader reader = null;
InputStream inputStream = null;
HttpURLConnection httpURLConnection = null;
try {
URL httpUrl = new URL(url);
URLConnection connection = httpUrl.openConnection();
httpURLConnection = (HttpURLConnection) connection;
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
httpURLConnection.setReadTimeout(30000);// 无响应30秒,断开会话
httpURLConnection.setConnectTimeout(30000);
httpURLConnection.setRequestMethod("GET");
httpURLConnection.setRequestProperty("Referer","http://taobao.51bi.com");
httpURLConnection.setRequestProperty("Accept-Language","zh-cn");
httpURLConnection.setRequestProperty("User-Agent","Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0; MALC)");
httpURLConnection.connect();
inputStream = httpURLConnection.getInputStream();// 发送连接
reader = new BufferedReader(new InputStreamReader(inputStream,"utf-8"));
String line = null;
while ((line = reader.readLine()) != null) {
josn.append(line);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if(reader != null)
reader.close();
if(inputStream != null)
inputStream.close();
if(httpURLConnection != null)
httpURLConnection.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
return josn.toString().trim();
}



post 发送数据xml 或者json 然后读取返回值

public String getJosnByUrl(String url,String param) {
StringBuffer josn = new StringBuffer();
BufferedReader reader = null;
PrintWriter out=null;
InputStream inputStream = null;
HttpURLConnection httpURLConnection = null;
try {
URL httpUrl = new URL(url);
URLConnection connection = httpUrl.openConnection();
httpURLConnection = (HttpURLConnection) connection;
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
httpURLConnection.setReadTimeout(30000);// 无响应30秒,断开会话
httpURLConnection.setConnectTimeout(30000);
httpURLConnection.setRequestMethod("POST");

//httpURLConnection.setRequestProperty("Referer","http://taobao.51bi.com");
httpURLConnection.setRequestProperty("Accept-Language","zh-cn");
httpURLConnection.setRequestProperty("User-Agent","Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0; MALC)");
httpURLConnection.connect();
out=new PrintWriter(httpURLConnection.getOutputStream());
out.write(param);//数据写入缓存中
out.flush();
inputStream = httpURLConnection.getInputStream();// 发送连接

reader = new BufferedReader(new InputStreamReader(inputStream,"utf-8"));
String line = null;
while ((line = reader.readLine()) != null) {
josn.append(line);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if(out!=null)
out.close();
if(reader != null)
reader.close();
if(inputStream != null)
inputStream.close();
if(httpURLConnection != null)
httpURLConnection.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
return josn.toString().trim();
}
相关标签: URL