跪求例子、、、、、HttpClient
解决方案 »
- 求教:关于servletconfig的问题
- ssh 配置事务处理 本地测试通过 服务器无法访问 急急 在线等
- 我该选择哪个linux版本啊?
- javabean能不能通过其中的方法来设置和得到其属性!
- 2个月时间,请问学JAVA好,还是考网络工程师好
- 与ACCESS连接的问题,,大家快来帮忙呀!
- 各位请问JSP+Servlet+JavaBean支不支持微软的Access数据库
- 不能forward请求的问题?求救!!!----在线等
- 关于邮件发送的问题!! 高分!!!!!!!!!!
- EJB ;建了個helloEjb ,運行jsp文件時出現以下錯誤,請高手指教!!謝謝
- Hibernate 关于many-to-many
- Unresolved compilation problems求大神解决
/**
* 测试返回JSON数据
* @param session
* @return
*/
@RequestMapping(value="/test")
@ResponseBody
public Object test(HttpSession session){
return session.getAttribute("permit");
}
{
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);
}
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();
}
}
比如腾讯视频: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"}
报什么错?我测试楼上的是可以的。{"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}]}