#region 私有字段
privatestring responseString;
privatestaticstring key;
privatestaticstring url;
privatestaticstring session;
privatestaticstring token;
privatestaticstring nick;
#endregion
///
/// 获取身份码 和识别码
///
///
publicstring GetUniqueID()
{
byte[] byteArray = DeviceExtendedProperties.GetValue("DeviceUniqueId") asbyte[];
string strTemp ="";
string strDeviceUniqueID ="";
foreach (byte bt in byteArray)
{
strTemp = bt.ToString();
if (1== strTemp.Length)
{
strTemp ="00"+ strTemp;
}
elseif (2== strTemp.Length)
{
strTemp ="0"+ strTemp;
}
strDeviceUniqueID += strTemp;
}
return strDeviceUniqueID;
}
///
/// 将身份码和识别码以字典集合的形式返回
///
///
///
public List<string> GetStr(string str)
{
List<string> strList =new List<string>();
strList.Add(str.Substring(0, 15));
strList.Add(str.Substring(14, 15));
return strList;
}
///
/// 得到签名
///
///
///
///
publicstring CreateSign(string appkey, string appsecret)
{
string str = appkey + appsecret + DateTime.Now.Millisecond;
MD5Helper MD5 = MD5Helper.Create();
str = Convert.ToBase64String(MD5.ComputeHash(Encoding.UTF8.GetBytes(str)));
return str;
}
///
/// Load事件
///
///
///
privatevoid PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
{
//登陆第一步 调用第一个接口:com.taobao.wireless.mtop.getLoginUrl
List<string> strList = GetStr(GetUniqueID());
string url ="http://api.m.taobao.com/rest/api2.do?api=com.taobao.wireless.mtop.getLoginUrl";
string ttid ="&ttid=400000_12315576@wp7stk_windowsphone7_1.0.0";
string data ="&data={%22appkey%22:%2212315576%22}";
url +="&v=windowsphone7Client";
url += ttid;
url +="&imei=";
url += strList[0].ToString();
url +="&imsi=";
url += strList[1].ToString();
url += data;
url +="&sign=";
url += CreateSign("12315576", "5734907fbdd9af8f23493a9411ba9805");
//----------------------------------------以上为字符串的拼接,调用几个方法最终将url拼装成需要的请求的地址
//WebBrowserTask web = new WebBrowserTask();
//web.URL = url;
//web.Show();
//-------------------------------------以下为发出请求,切忌,此步单单是请求,并没有发送请求的数据流
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.ContentType ="application/x-www-form-urlencoded;charset=utf-8";
request.Method ="POST";
request.BeginGetResponse(new AsyncCallback(GetResponseCallback), request);
}
privatevoid GetResponseCallback(IAsyncResult asynchronousResult)
{
//try
//{
HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asynchronousResult);
Stream streamResponse = response.GetResponseStream();
StreamReader streamRead =new StreamReader(streamResponse);
responseString = streamRead.ReadToEnd(); //以字符串形式接收json数据
//---------------------------------------------------注意此步为解析json数据,在本页底部为此定义了Text和Data两个实体类
byte[] b = Encoding.UTF8.GetBytes(responseString);
MemoryStream stream =new MemoryStream(b);
DataContractJsonSerializer data =new DataContractJsonSerializer(typeof(Text));
Text t = (Text)data.ReadObject(stream);
key = t.Data.Key;
url = t.Data.Url;
//------------------------------------------------------------------------------------------------------------------
streamResponse.Close();
streamRead.Close();
response.Close();
Dispatcher.BeginInvoke(
() =>
{
this.webBLoad.Navigate(new Uri(url));
MessageBox.Show("请在登录后授权本软件!", "温馨提示", MessageBoxButton.OK);
});
//}
//catch
//{
// MessageBox.Show("网络连接有误!");
//}
}
privatevoid GetResponseCall(IAsyncResult asynchronousResult)
{
HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asynchronousResult);
Stream streamResponse = response.GetResponseStream();
StreamReader streamRead =new StreamReader(streamResponse);
responseString = streamRead.ReadToEnd();
//---------------------------------------------------------此部分也是解析json数据,在本页底部也为此定义了Test和data两个实体类
byte[] b = Encoding.UTF8.GetBytes(responseString);
MemoryStream stream =new MemoryStream(b);
DataContractJsonSerializer data =new DataContractJsonSerializer(typeof(Test));
Test t = (Test)data.ReadObject(stream);
token = t.Data.Token;
session = t.Data.Topsession;
nick = t.Data.Nick;
//-----------------------------------------------------------------------------------------------------
LoadBLL.GetLoad(nick, session);
streamResponse.Close();
streamRead.Close();
response.Close();
Dispatcher.BeginInvoke(
() =>
{
if (LoadBLL.Load.Count ==0|| LoadBLL.Load[0] ==null)
{
MessageBox.Show("登录授权失败!");
NavigationService.GoBack();
}
else
{
NavigationService.GoBack();
}
});
}
privatevoid webBLoad_Navigated(object sender, System.Windows.Navigation.NavigationEventArgs e)
{
if (e.Uri.AbsolutePath.Equals("/mtop/bind_app_result.htm"))
{
List<string> strList = GetStr(GetUniqueID());
string uri ="http://api.m.taobao.com/rest/api2.do?api=com.taobao.client.mtop.getUserSessionKey";
string ttid ="&ttid=400000_12315576@wp7stk_windowsphone7_1.0.0";
string data ="&data={\"key\":\""+ key +"\",\"appkey\":\"12315576\"}";
uri +="&v=windowsphone7Client";
uri += ttid;
uri +="&imei=";
uri += strList[0].ToString();
uri +="&imsi=";
uri += strList[1].ToString();
uri += data;
uri +="&sign=";
uri += CreateSign("12315576", "5734907fbdd9af8f23493a9411ba9805");
this.webBLoad. Navigate(new Uri(uri));
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
request.ContentType ="application/x-www-form-urlencoded;charset=utf-8";
request.Method ="POST";
request.BeginGetResponse(new AsyncCallback(GetResponseCall), request);
}
}
privatevoid PhoneApplicationPage_OrientationChanged(object sender, OrientationChangedEventArgs e)
{
PageOrientation ori=this.Orientation;
if (ori == PageOrientation.Portrait || ori == PageOrientation.PortraitDown || ori == PageOrientation.PortraitUp || ori == PageOrientation.None)
{
this.webBLoad.Height =720;
this.webBLoad.Width=480;
}
if (ori == PageOrientation.Landscape || ori == PageOrientation.LandscapeLeft || ori == PageOrientation.LandscapeRight)
{
this.webBLoad.Width =730;
this.webBLoad.Height =430;
}
}
}
几个在登录功能用到的类的命名空间:using System.Runtime.Serialization;
View Code
[DataContract]
publicclass Text
{
privatestring _api;
privatestring _v;
privatestring _ret;
private Data _data;
[DataMember(Name ="api")]
publicstring Api
{
get { return _api; }
set { _api = value; }
}
[DataMember(Name ="v")]
publicstring V
{
get { return _v; }
set { _v = value; }
}
[DataMember(Name ="ret")]
publicstring Ret
{
get { return _ret; }
set { _ret = value; }
}
[DataMember(Name ="data")]
public Data Data
{
get { return _data; }
set { _data = value; }
}
}
[DataContract]
publicclass Data
{
privatestring _key;
privatestring _url;
[DataMember(Name ="key")]
publicstring Key
{
get { return _key; }
set { _key = value; }
}
[DataMember(Name ="url")]
publicstring Url
{
get { return _url; }
set { _url = value; }
}
}
[DataContract]
publicclass Test
{
privatestring _api;
privatestring _v;
privatestring _ret;
private data _data;
[DataMember(Name ="api")]
publicstring Api
{
get { return _api; }
set { _api = value; }
}
[DataMember(Name ="v")]
publicstring V
{
get { return _v; }
set { _v = value; }
}
[DataMember(Name ="ret")]
publicstring Ret
{
get { return _ret; }
set { _ret = value; }
}
[DataMember(Name ="data")]
public data Data
{
get { return _data; }
set { _data = value; }
}
}
[DataContract]
publicclass data
{
privatestring _topsession;
privatestring _sid;
privatestring _time;
privatestring _ecode;
privatestring _nick;
privatestring _token;
[DataMember(Name ="topsession")]
publicstring Topsession
{
get { return _topsession; }
set { _topsession = value; }
}
[DataMember(Name ="sid")]
publicstring Sid
{
get { return _sid; }
set { _sid = value; }
}
[DataMember(Name ="time")]
publicstring Time
{
get { return _time; }
set { _time = value; }
}
[DataMember(Name ="ecode")]
publicstring Ecode
{
get { return _ecode; }
set { _ecode = value; }
}
[DataMember(Name ="nick")]
publicstring Nick
{
get { return _nick; }
set { _nick = value; }
}
[DataMember(Name ="token")]
publicstring Token
{
get { return _token; }
set { _token = value; }
}
}
UI: