HTTP URLConnection 發送get請求

    String urlstr="servie="+URLEncoder.encode(serivce,"UTF-8") +"&partener="+URLEncoder.encode(partener,"UTF-8");

    String newUrl="http://10.53.78.116:80/gateway.do?"+urlstr;

    URL realUrl= new URL(newUrl);

    URLConnection connect=realUrl.openConnection();

    //設置請求超時

    connection.setConnectionTimeOut(6000);

    //設置通用的請求屬性頭

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

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

    connection.setRequestProperty("user-agent","Mozilla/4.0(compatiable;MSIE 6.0; Windows NT 5.1; sv1)");

    connection.connect();

   //設置響應超時

   connection.setReadTime(6000);

   //獲取所有響應頭字段

    Map<String,List<String>> map= connection.getHeaderFields();

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

         system,out.println(map.getKey(key));

       }

 if(conn.getResponseCode()==200){//證明發送成功

                     return true;

           }

    //定義bufferedReader字符流讀取URL的響應
    BufferedReader bred=new BufferedReader(new InputStreamReader(connection.getInputStream()));

    String line="";

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

       str+=line

      }

   syso....(str);

 

   關閉 流

    bred.close();

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章