import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
public class Test {
public static void main(String args[]) throws UnknownHostException, IOException{
try {
String address = "上海大剧院";
String city = "上海";
Socket s = new Socket("api.map.baidu.com",80);
BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream(),"UTF-8"));
OutputStream out = s.getOutputStream();
StringBuffer sb = new StringBuffer("GET /geocoder?address="+address+"&output=json&key=f8d6ec3d5c12e9f8a9b227f4d2a39a93&city="+city+" HTTP/1.1\r\n");
sb.append("User-Agent: Java/1.6.0_20\r\n");
sb.append("Host: api.map.baidu.com:80\r\n");
sb.append("Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2\r\n");
sb.append("Connection: Close\r\n");
sb.append("\r\n");
out.write(sb.toString().getBytes());
String json ="";
String tmp = "";
while((tmp = br.readLine())!=null){
System.out.println(tmp);
json += tmp;
}
System.out.println(json);
out.close();
br.close();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
java socket 模拟http请求问题
最新推荐文章于 2024-03-09 21:59:01 发布