您的位置:

java请求url,java请求url并取得返回json

本文目录一览:

java中 如何获取客户端请求的url

在servlet中的request对象中有url,可以用方法 getRequestURI().

如果在程序中得不到该请求的request对象 那就得不到。

所以得到url的 关键是 先得到 request

java远程请求url地址,并且要求传递参数,应该怎么实现

public String getHTML(String httpUrl, String Charset) {

String html = "";

try {

URL url = new URL(httpUrl.toString());

StringBuffer document = new StringBuffer();

try {

URLConnection urlCon = (HttpURLConnection) url.openConnection();

BufferedReader reader = new BufferedReader(

new InputStreamReader(urlCon.getInputStream()));

String Result = "";

while ((Result = reader.readLine()) != null) {

document.append(Result);

// System.out.println(Result);

}

html = document.toString();

} catch (IOException e) {

html = "服务未响应";

}

} catch (MalformedURLException e) {

html = "不支持的协议";

}

return html;

}

Java请求一个URL。获取网站返回的数据。

public static String SendGET(String url,String param){

   String result="";//访问返回结果

   BufferedReader read=null;//读取访问结果

   

   try {

    //创建url

    URL realurl=new URL(url+"?"+param);

    //打开连接

    URLConnection connection=realurl.openConnection();

     // 设置通用的请求属性

             connection.setRequestProperty("accept", "*/*");

             connection.setRequestProperty("connection", "Keep-Alive");

             connection.setRequestProperty("user-agent",

                     "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");

             //建立连接

             connection.connect();

          // 获取所有响应头字段

             MapString, ListString map = connection.getHeaderFields();

             // 遍历所有的响应头字段,获取到cookies等

             for (String key : map.keySet()) {

                 System.out.println(key + "---" + map.get(key));

             }

             // 定义 BufferedReader输入流来读取URL的响应

             read = new BufferedReader(new InputStreamReader(

                     connection.getInputStream(),"UTF-8"));

             String line;//循环读取

             while ((line = read.readLine()) != null) {

                 result += line;

             }

   } catch (IOException e) {

    e.printStackTrace();

   }finally{

    if(read!=null){//关闭流

     try {

      read.close();

     } catch (IOException e) {

      e.printStackTrace();

     }

    }

   }

    

   return result; 

 }

java建立url请求 服务器怎么写

//get请求

public String get(String url){

HttpURLConnection conn = null;

BufferedReader rd = null ;

StringBuilder sb = new StringBuilder ();

String line = null ;

String response = null;

try {

conn = (HttpURLConnection) new URL(url).openConnection();

conn.setRequestMethod("GET");

conn.setDoInput(true);

//conn.setReadTimeout(20000);

//conn.setConnectTimeout(20000);

conn.setUseCaches(false);

conn.connect();

rd = new BufferedReader( new InputStreamReader(conn.getInputStream(), "UTF-8"));

while ((line = rd.readLine()) != null ) {

sb.append(line);

}

response = sb.toString();

} catch (MalformedURLException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}finally{

try {

if(rd != null){

rd.close();

}

if(conn != null){

conn.disconnect();

}

} catch (IOException e) {

e.printStackTrace();

}

}

return response;

}

//post表单请求

public String post(String url, MapString, String form){

HttpURLConnection conn = null;

PrintWriter pw = null ;

BufferedReader rd = null ;

StringBuilder out = new StringBuilder();

StringBuilder sb = new StringBuilder();

String line = null ;

String response = null;

for (String key : form.keySet()) {

if(out.length()!=0){

out.append("");

}

out.append(key).append("=").append(form.get(key));

}

try {

conn = (HttpURLConnection) new URL(url).openConnection();

conn.setRequestMethod("POST");

conn.setDoOutput(true);

conn.setDoInput(true);

//conn.setReadTimeout(20000);

//conn.setConnectTimeout(20000);

conn.setUseCaches(false);

conn.connect();

pw = new PrintWriter(conn.getOutputStream());

pw.print(out.toString());

pw.flush();

rd = new BufferedReader( new InputStreamReader(conn.getInputStream(), "UTF-8"));

while ((line = rd.readLine()) != null ) {

sb.append(line);

}

response = sb.toString();

} catch (MalformedURLException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}finally{

try {

if(pw != null){

pw.close();

}

if(rd != null){

rd.close();

}

if(conn != null){

conn.disconnect();

}

} catch (IOException e) {

e.printStackTrace();

}

}

return response;

}