我用java去封装一些微信的接口,当扫码登录以后,微信会返回skey
wxsid
wxuin
这三个东西,然后再把这三个东西post出去,就可以拿到用户信息
String BaseRequest = "{\"BaseRequest\":{\"Uin\":\""+wxuin+"\",\"Sid\":\""+wxsid+"\",\"Skey\":\""+skey+"\",\"DeviceID\":\"e159973572418266\"}}";
res = Http.sendPost("https://wx2.qq.com/cgi-bin/mmwebwx-bin/webwxinit",BaseRequest);
可是这样post以后返回的数据却是这样的
{
"BaseResponse": {
"Ret": 1100,
"ErrMsg": ""
},
"Count": 0,
"ContactList": [],
"SyncKey": {
"Count": 0,
"List": []
},
"User": {
"Uin": 0,
"UserName": "",
"NickName": "",
"HeadImgUrl": "",
"RemarkName": "",
"PYInitial": "",
"PYQuanPin": "",
"RemarkPYInitial": "",
"RemarkPYQuanPin": "",
"HideInputBarFlag": 0,
"StarFriend": 0,
"Sex": 0,
"Signature": "",
"AppAccountFlag": 0,
"VerifyFlag": 0,
"ContactFlag": 0,
"WebWxPluginSwitch": 0,
"HeadImgFlag": 0,
"SnsFlag": 0
},
"ChatSet": "",
"SKey": "",
"ClientVersion": 0,
"SystemTime": 0,
"GrayScale": 0,
"InviteStartCount": 0,
"MPSubscribeMsgCount": 0,
"MPSubscribeMsgList": [],
"ClickReportInterval": 0
}
我用的post方法如下
//用来发送json
public static String sendPost(String url,String Params)throws IOException{
OutputStreamWriter out = null;
BufferedReader reader = null;
String response="";
try {
URL httpUrl = null; //HTTP URL类 用这个类来创建连接
//创建URL
httpUrl = new URL(url);
//建立连接
HttpURLConnection conn = (HttpURLConnection) httpUrl.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/json");
conn.setRequestProperty("connection", "keep-alive");
conn.setUseCaches(false);//设置不要缓存
conn.setInstanceFollowRedirects(true);
conn.setDoOutput(true);
conn.setDoInput(true);
conn.connect();
//POST请求
out = new OutputStreamWriter(
conn.getOutputStream());
out.write(Params);
out.flush();
//读取响应
reader = new BufferedReader(new InputStreamReader(
conn.getInputStream()));
String lines;
while ((lines = reader.readLine()) != null) {
lines = new String(lines.getBytes(), "utf-8");
response+=lines;
}
reader.close();
// 断开连接
conn.disconnect();
//log.info(response.toString());
} catch (Exception e) {
System.out.println("发送 POST 请求出现异常!"+e);
e.printStackTrace();
}
//使用finally块来关闭输出流、输入流
finally{
try{
if(out!=null){
out.close();
}
if(reader!=null){
reader.close();
}
}
catch(IOException ex){
ex.printStackTrace();
}
}
return response;
}
BaseRequest是json,格式不正确