跪求例子、、、、、HttpClient

解决方案 »

  1.   

    spring mvc 的话,这样:
    /** 
         * 测试返回JSON数据 
         * @param session 
         * @return 
         */  
        @RequestMapping(value="/test")  
        @ResponseBody  
        public Object test(HttpSession session){  
               
            return session.getAttribute("permit");  
        }  
      

  2.   

    用class类,直接控制台执行,访问网址,然后得到返回值,我这有一个C#的,要和他类似的java的代码,求例子: private string doSendMessage(string wx_username, string wx_token, string fake_id, string content)
            {
                string sURL = "http://gxyweixin.duapp.com/SendMessage?";
                string param = String.Format("name={0}&password={1}&fakeid={2}&content={3}", wx_username, wx_token, fake_id, HttpUtility.UrlEncode(content));
                byte[] bs = Encoding.ASCII.GetBytes(param);            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(sURL);
                request.Method = "POST";
                request.ContentType = "application/x-www-form-urlencoded";
                request.ContentLength = bs.Length;
                Stream reqStream = request.GetRequestStream();
                reqStream.Write(bs, 0, bs.Length);            Stream objStream = request.GetResponse().GetResponseStream();
                StreamReader objReader = new StreamReader(objStream);            string response_data = objReader.ReadToEnd();            request = null;
                textpost.Text = stringify(response_data);
                return stringify(response_data);
            }
      

  3.   

    一个简略的例子:package com.ljn.base;import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.net.MalformedURLException;
    import java.net.URL;
    import java.net.URLConnection;/**
     * @author lijinnan
     * @date:2013-11-11 下午6:12:26  
     */
    public class HttpJSONTest {    /**
         * @param args
         */
        public static void main(String[] args) {
            String url = "http://api.geonames.org/citiesJSON?north=44.1&south=-9.9&east=-22.4&west=55.2&lang=de&username=demo";
            String json = loadJSON(url);
            System.out.println(json);
        }    public static String loadJSON (String url) {
            StringBuilder json = new StringBuilder();
            try {
                URL oracle = new URL(url);
                URLConnection yc = oracle.openConnection();
                BufferedReader in = new BufferedReader(new InputStreamReader(
                                            yc.getInputStream()));
                String inputLine = null;
                while ( (inputLine = in.readLine()) != null) {
                    json.append(inputLine);
                }
                in.close();
            } catch (MalformedURLException e) {
            } catch (IOException e) {
            }
            return json.toString();
        }
    }
      

  4.   

    通过jsp或servlet可以轻松实现,servlet获取打印流,out.print();上两周写的获取网络视频源地址,输入视频播放地址就可获取视频源地址,可直接下载。
    比如腾讯视频:http://v.qq.com/cover/j/jowk63wbewcbatz.html
    在浏览器敲如下地址:
    http://60.206.40.187:8080/hynet_video/1/getVideoResource?pageUrl=http://v.qq.com/cover/j/jowk63wbewcbatz.html返回JSON字符串:{"resourceURL":"http://124.193.165.207/vlive.qqvideo.tc.qq.com/j0013becfin.mp4?vkey=488EFCE405EDD37E356562D686FD10F1312DED60186FA4B74CF3B87AA9E3099EA13DC7B9CB819426&br=66&platform=1&fmt=mp4&level=3","requestURL":"http://v.qq.com/cover/j/jowk63wbewcbatz.html"}
      

  5.   

    我测试了一下用我要用的借口,但是在 BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream()));这里直接弹出来了
      

  6.   

    刚回复错了我测试了一下用我要用的借口,但是在 BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream()));这里直接弹出来了
      

  7.   

    在 BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream()));这里直接弹出来了
    报什么错?我测试楼上的是可以的。{"geonames":[{"fcodeName":"capital of a political entity","toponymName":"Mexico City","countrycode":"MX","fcl":"P","fclName":"city, village,...","name":"Mexiko-Stadt","wikipedia":"en.wikipedia.org/wiki/Mexico_City","lng":-99.12766456604,"fcode":"PPLC","geonameId":3530597,"lat":19.428472427036,"population":12294193},{"fcodeName":"capital of a political entity","toponymName":"Manila","countrycode":"PH","fcl":"P","fclName":"city, village,...","name":"Manila","wikipedia":"en.wikipedia.org/wiki/Manila","lng":120.9822,"fcode":"PPLC","geonameId":1701668,"lat":14.6042,"population":10444527},{"fcodeName":"capital of a political entity","toponymName":"Dhaka","countrycode":"BD","fcl":"P","fclName":"city, village,...","name":"Dhaka","wikipedia":"en.wikipedia.org/wiki/Dhaka","lng":90.40743827819824,"fcode":"PPLC","geonameId":1185241,"lat":23.710395616597037,"population":10356500},{"fcodeName":"capital of a political entity","toponymName":"Seoul","countrycode":"KR","fcl":"P","fclName":"city, village,...","name":"Seoul","wikipedia":"en.wikipedia.org/wiki/Seoul","lng":126.977834701538,"fcode":"PPLC","geonameId":1835848,"lat":37.5682561388953,"population":10349312},{"fcodeName":"capital of a political entity","toponymName":"Jakarta","countrycode":"ID","fcl":"P","fclName":"city, village,...","name":"Jakarta","wikipedia":"en.wikipedia.org/wiki/Jakarta","lng":106.84513092041016,"fcode":"PPLC","geonameId":1642911,"lat":-6.214623197035775,"population":8540121},{"fcodeName":"capital of a political entity","toponymName":"Tokyo","countrycode":"JP","fcl":"P","fclName":"city, village,...","name":"Tokio","wikipedia":"en.wikipedia.org/wiki/Tokyo","lng":139.69171,"fcode":"PPLC","geonameId":1850147,"lat":35.6895,"population":8336599},{"fcodeName":"capital of a political entity","toponymName":"Taipei","countrycode":"TW","fcl":"P","fclName":"city, village,...","name":"Taipeh","wikipedia":"en.wikipedia.org/wiki/Taipei_Railway_Station","lng":121.531846,"fcode":"PPLC","geonameId":1668341,"lat":25.047763,"population":7871900},{"fcodeName":"capital of a political entity","toponymName":"Bogotá","countrycode":"CO","fcl":"P","fclName":"city, village,...","name":"Bogotá","wikipedia":"en.wikipedia.org/wiki/Bogot%C3%A1","lng":-74.08175468444824,"fcode":"PPLC","geonameId":3688689,"lat":4.609705849789108,"population":7674366},{"fcodeName":"capital of a political entity","toponymName":"Beijing","countrycode":"CN","fcl":"P","fclName":"city, village,...","name":"Peking","wikipedia":"en.wikipedia.org/wiki/Beijing","lng":116.397228240967,"fcode":"PPLC","geonameId":1816670,"lat":39.9074977414405,"population":7480601},{"fcodeName":"capital of a political entity","toponymName":"Hong Kong","countrycode":"HK","fcl":"P","fclName":"city, village,...","name":"Hong Kong","wikipedia":"en.wikipedia.org/wiki/Hong_Kong","lng":114.157691001892,"fcode":"PPLC","geonameId":1819729,"lat":22.2855225817732,"population":7012738}]}