java socket 模拟http请求问题

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();  
        }  
	}
}   
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值