接收字符串
两种格式 get, post需要设置发送参数
public static void interfaceUtil(String path,String data) {
try {
URL url = new URL(path);
//打开和url之间的连接
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
PrintWriter out = null;
/**设置URLConnection的参数和普通的请求属性****start***/
conn.setRequestProperty("accept", "*/*");
conn.setRequestProperty("connection", "Keep-Alive");
conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");
/**设置URLConnection的参数和普通的请求属性****end***/
//设置是否向httpUrlConnection输出,设置是否从httpUrlConnection读入,此外发送post请求必须设置这两个
//最常用的Http请求无非是get和post,get请求可以获取静态页面,也可以把参数放在URL字串后面,传递给servlet,
//post与get的 不同之处在于post的参数不是放在URL字串里面,而是放在http请求的正文内。
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setRequestMethod("GET");//GET和POST必须全大写
/**GET方法请求*****start*/
/**
* 如果只是发送GET方式请求,使用connet方法建立和远程资源之间的实际连接即可;
* 如果发送POST方式的请求,需要获取URLConnection实例对应的输出流来发送请求参数。
* */
conn.connect();
/**GET方法请求*****end*/
/***POST方法请求****start*/
// out = new PrintWriter(conn.getOutputStream());//获取URLConnection对象对应的输出流
//
// out.print(data);//发送请求参数即数据
//
// out.flush();//缓冲数据
/***POST方法请求****end*/
//获取URLConnection对象对应的输入流
InputStream is = conn.getInputStream();
//构造一个字符流缓存
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String str = "";
while ((str = br.readLine()) != null) {
str=new String(str.getBytes(),"UTF-8");//解决中文乱码问题
System.out.println(str);
}
//关闭流
is.close();
//断开连接,最好写上,disconnect是在底层tcp socket链接空闲时才切断。如果正在被其他线程使用就不切断。
//固定多线程的话,如果不disconnect,链接会增多,直到收发不出信息。写上disconnect后正常一些。
conn.disconnect();
System.out.println("完整结束");
} catch (Exception e) {
e.printStackTrace();
}
}
如果是json数据用上面的汉字会乱码,这个是Json格式数据
public static JSONObject getXpath(String requestUrl){
String res="";
JSONObject object = null;
StringBuffer buffer = new StringBuffer();
try{
URL url = new URL(requestUrl);
HttpURLConnection urlCon= (HttpURLConnection)url.openConnection();
if(200==urlCon.getResponseCode()){
InputStream is = urlCon.getInputStream();
InputStreamReader isr = new InputStreamReader(is,"utf-8");
BufferedReader br = new BufferedReader(isr);
String str = null;
while((str = br.readLine())!=null){
buffer.append(str);
}
br.close();
isr.close();
is.close();
res = buffer.toString();
//JsonParser parse =new JsonParser();
object = JSONObject.fromObject(res);
return object;
}
}catch(IOException e){
e.printStackTrace();
}
//return object;
return null;
}
mavn 中json 依赖包
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
<version>1.9.3</version>
</dependency>
<dependency>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
<version>3.2.1</version>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>net.sf.ezmorph</groupId>
<artifactId>ezmorph</artifactId>
<version>1.0.6</version>
</dependency>
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>2.2.3</version>
<classifier>jdk15</classifier><!-- 指定jdk版本
</dependency>