远程post请求

Ajax跨域获取远程的json数组,通过servlet中转一下,servlet可以这样写:

之前用的是get方式,但是,json数组太长,通过流的方式数据没有取完就返回给servlet,所以改用下面的方法:(有效)

public void doPost(HttpServletRequest req, HttpServletResponse resp)  
            throws ServletException, IOException {  
                boolean requestType = true;
                resp.setContentType("text/html;charset=utf-8");
                PrintWriter out = resp.getWriter();  
                String wapHost=PropertyManager.getProperty("wap.mobile.host");
                String msource=wapHost+"productdetail.do";
                String page=req.getParameter("page");
                String pid=req.getParameter("id");
                msource=msource+"?pid="+pid+"&page="+page;

                String url=msource;            
                HttpClient client = new HttpClient();  
                HttpMethod method = null;  
                if(requestType){//判断请求方式,并实例化HttpMethod对象,true:POST,false:GET  
                    method = new UTF8PostMethod(url);  
                }else{  
                    method = new GetMethod(url);  
                }         
                client.executeMethod(method);//执行请求  
                String bodystr = method.getResponseBodyAsString();//返回结果  
                out.println(bodystr);//将结果返回给客户端  
        }  
        
        /**
        * 内部类,转换URL字符串为UTF-8
        * @author Administrator
        *
        */  
        private static class UTF8PostMethod extends PostMethod {   
            public UTF8PostMethod(String url) {   
                super(url);   
            }   
            @Override   
            public String getRequestCharSet() {   
                return "UTF-8";   
            }   
        } 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值