package com.actions;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;
import java.util.concurrent.TimeUnit;
import org.apache.http.HttpResponse;
import org.apache.http.HttpVersion;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.params.ClientPNames;
import org.apache.http.client.params.CookiePolicy;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.CoreProtocolPNames;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.json.JSONException;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
public class WeChat {
public static String readContentFromGet(String jsonData,String GET_URL) throws Exception{
// 拼凑get请求的URL字串,使用URLEncoder.encode对特殊和不可见字符进行编码
String getURL = "";
if(!"".equals(jsonData)){
getURL = GET_URL + "?jsonData=" + URLEncoder.encode(jsonData, "utf-8");
}else{
getURL = GET_URL;
}
URL getUrl = new URL(getURL);
// 根据拼凑的URL,打开连接,URL.openConnection函数会根据URL的类型,
// 返回不同的URLConnection子类的对象,这里URL是一个http,因此实际返回的是HttpURLConnection
HttpURLConnection connection = (HttpURLConnection) getUrl.openConnection();
// 进行连接,但是实际上get request要在下一句的connection.getInputStream()函数中才会真正发到
// 服务器
connection.setConnectTimeout(25000);
connection.setReadTimeout(25000);
connection.connect();
// 取得输入流,并使用Reader读取
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(),"utf-8"));
String lines;
String jsonString = "";
while ((lines = reader.readLine()) != null) {
jsonString = jsonString+lines;
}
reader.close();
// 断开连接
connection.disconnect();
return jsonString;
}
public void testJson22(){
//String APPID="wx5744034e5b8dda97";
//String APPSECRET="466d693b8d9812098f05483bb59a962a";
String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx5744034e5b8dda97&secret=466d693b8d9812098f05483bb59a962a";
try {
WeChat t=new WeChat();
String result =t.readContentFromGet("",url);
System.out.println("result:"+result);
JSONObject json = JSONObject.fromObject(result);
System.out.println(json.toString());
} catch (Exception e) {
}
}
public void testJson23(){
//String APPID="wx5744034e5b8dda97";
//String APPSECRET="466d693b8d9812098f05483bb59a962a";
String url = "https://api.weixin.qq.com/cgi-bin/groups/get?access_token=XCWx7i2C5iN09R-0B0vRc6tc3VDXZ3zGrPUf-EQzWVjyiVhn6rK2r3dsaCRpP_YegoCIeLDRFusNdA7T58f-lYOXbOQSPJztTUvx8ZgWnhgYQZbABAKCL";
try {
WeChat t=new WeChat();
String jsons2="{\"group\":{\"name\":\"test\"}}";
String result =t.readContentFromGet("",url);
System.out.println("result:"+result);
JSONObject json = JSONObject.fromObject(result);
System.out.println(json.toString());
String id= json.getString("id ");
String name= json.getString("name");
String count= json.getJSONObject("group").getString("count");
System.out.println("11111111111111111111-------------------------------------------------------------------------------");
System.out.println(id);
System.out.println(name);
System.out.println(count);
} catch (Exception e) {
}
}
public void testJson24() {
try {
HttpClient httpclient = new DefaultHttpClient();
// 设置Cookie的兼容性
httpclient.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.BROWSER_COMPATIBILITY);
httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
httpclient.getParams().setParameter("http.protocol.content-charset",HTTP.UTF_8);
httpclient.getParams().setParameter(HTTP.CONTENT_ENCODING, HTTP.UTF_8);
httpclient.getParams().setParameter(HTTP.CHARSET_PARAM,HTTP.UTF_8);
httpclient.getParams().setParameter(HTTP.DEFAULT_PROTOCOL_CHARSET,HTTP.UTF_8);
// 15秒连接不上则失败
httpclient.getConnectionManager().closeIdleConnections(15, TimeUnit.SECONDS);
HttpPost httppost = new HttpPost("回传地址
String jsons2="{\"group\":{\"name\":\"test2\"}}";
System.out.println("进来了");
JSONObject json=JSONObject.fromObject(jsons2);
System.out.println("进来了11111111");
HttpResponse response;
StringEntity entity;
entity = new StringEntity(json.toString(),"utf-8");
entity.setContentEncoding("UTF-8");
entity.setContentType("application/json");
httppost.setEntity(entity);
response = httpclient.execute(httppost);
String result=EntityUtils.toString(response.getEntity(),"utf8");
System.out.println(result);
JSONObject jsonResult=JSONObject.fromObject(result);
System.out.println("1111111111111111111111111111111111111111111111111111111111111");
System.out.println(jsonResult.getJSONObject("group").get("id"));
System.out.println(jsonResult.getJSONObject("group").get("name"));
System.out.println("1111111111111111111111111111111111111111111111111111111111111");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}//解决中文乱码问题
catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}