其他配置看之前的博文,这里记录将微信图片转为本地磁盘保存
//获取用户信息
public String saveUserInfo(String openid,string uid)
{
//String url = "https://api.weixin.qq.com/sns/userinfo?access_token=" + GetAccess_Token() + "&openid=" + openid + "&lang=zh_CN";//这个是授权token
String url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=" + GetAccess_Token() + "&openid=" + openid + "&lang=zh_CN";//这个用基础token
string result = GetWebContent(url);
saveOpenID("获取用户微信个人信息:" + result);
WX_UserInfo jsonObj = JsonConvert.DeserializeObject<WX_UserInfo>(result);
SaveUserInfoWX(jsonObj,uid);
return result;
}
public void SaveUserInfoWX(WX_UserInfo jsonObj,string uid)
{
try
{
dbo conn = common.make();
conn.Update("update Tb_Common_User set Country='" + jsonObj.country + "', City='" + jsonObj.city + "', Sex='" + jsonObj.sex + "', NickName='" + jsonObj.nickname + "', Province='" + jsonObj.province + "',OpenID='" + jsonObj.openid + "', HeadImgUrl='" + jsonObj.headimgurl + "' where UserID='" + uid + "'");
string path = AppDomain.CurrentDomain.BaseDirectory + "weixin\\headImg\\";
string headimgurl = path + jsonObj.openid + ".jpg";
WriteBytesToFile(headimgurl, GetBytesFromUrl(jsonObj.headimgurl));
saveOpenID("保存微信个人信息成功!微信头像本地路径:" + headimgurl);
}
catch (Exception e)
{
saveOpenID("保存微信个人信息出错:" + e.Message);
throw;
}
}
static public byte[] GetBytesFromUrl(string url)
{
byte[] b;
HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create(url);
WebResponse myResp = myReq.GetResponse();
Stream stream = myResp.GetResponseStream();
//int i;
using (BinaryReader br = new BinaryReader(stream))
{
//i = (int)(stream.Length);
b = br.ReadBytes(500000);
br.Close();
}
myResp.Close();
return b;
}
//2、保存到磁盘文件中.
static public void WriteBytesToFile(string fileName, byte[] content)
{
try
{
FileStream fs = new FileStream(fileName, FileMode.Create);
BinaryWriter w = new BinaryWriter(fs);
try
{
w.Write(content);
}
finally
{
fs.Close();
w.Close();
}
}
catch (Exception)
{
throw;
}
}