import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.Socket; public class TalkClient { public static void main(String[] args) { try{ //向本机的4700端口发出客户请求 Socket socket=new Socket("restapi.amap.com",80); //由Socket对象得到输出流,并构造PrintWriter对象 PrintWriter os=new PrintWriter(socket.getOutputStream()); os.println("GET https://restapi.amap.com/v3/weather/weatherInfo?city=370883&key=。。。&output=xml"); os.flush(); //由Socket对象得到输入流,并构造相应的BufferedReader对象 BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream(), "UTF-8")); String line = null; while ((line = input.readLine()) != null) { System.out.println(line); } os.close(); //关闭Socket输出流 input.close(); //关闭Socket输入流 socket.close(); //关闭Socket }catch(Exception e){ e.printStackTrace();//出错,打印出错信息 } } }
来源:https://www.cnblogs.com/zhaideang/p/12290578.html