• wp7 httpwebrequest 和 ash1 加密


      1  public class CommonHelper
    2 {
    3 // 异常信息
    4 string _exception = "";
    5
    6
    7 //#define APP_KEY @"c40fe2f61bcfd611177be71ec305196b"
    8 //#define APP_SECRET @"5c585c08a597c23d"
    9
    10 //#define kSignUrl @"http://api.gozap.com/xauth/access_token"
    11 //#define kTokenUrl @"http://mu.api.gozap.com/xauth/access_token"
    12 //#define kRegisterUrl @"http://mu.api.gozap.com/service/register"
    13 //#else
    14 //#define kSignUrl @"http://api.gozap.com/xauth/access_token"
    15 //#define kTokenUrl @"http://api.gozap.com/xauth/access_token"
    16 //#define kRegisterUrl @"http://api.gozap.com/service/register"
    17
    18 private string APP_KEY = "c40fe2f61bcfd611177be71ec305196b";
    19 private string APP_SECRET = "5c585c08a597c23d";
    20 private string kSignUrl = @"http://api.gozap.com/xauth/access_token";
    21 private string kRegisterUrl = @"http://mu.api.gozap.com/service/register";
    22
    23 public string postData = null;
    24
    25 public void ConsumerRegist(string userName, string passWord, string strEmail)
    26 {
    27 string formStr = String.Format(@"oauth_consumer_key={0}&username={1}&password={2}&email={3}", APP_KEY, userName, passWord, strEmail);
    28
    29 System.Diagnostics.Debug.WriteLine(formStr);
    30
    31 string baseStr = APP_SECRET + "&" + APP_KEY + "&" + userName + "&" + passWord;
    32 string signature = SHA1_Hash(baseStr, APP_SECRET + "&");
    33 //signature = System.Convert.ToBase64String(signature);
    34 postData = formStr + "&oauth_signature=" + GetEncodedUri(signature);
    35
    36
    37 HttpWebRequest request = WebRequest.Create(
    38 new Uri(kRegisterUrl, UriKind.Absolute)) as HttpWebRequest;
    39
    40 request.Method = "POST";
    41
    42 IAsyncResult asyncResult = request.BeginGetRequestStream(new AsyncCallback(RequestStreamCallback), request);
    43 }
    44
    45 private void RequestStreamCallback(IAsyncResult result)
    46 {
    47 HttpWebRequest request = result.AsyncState as HttpWebRequest;
    48 request.ContentType = "application/x-www-form-urlencoded";
    49
    50 // HttpWebRequest.EndGetRequestStream(IAsyncResult) - 返回用于将数据写入某 URI 资源的 Stream
    51 Stream requestStream = request.EndGetRequestStream(result);
    52
    53 StreamWriter streamWriter = new StreamWriter(requestStream);
    54
    55 // byte[] postdata = System.Text.Encoding.UTF8.GetBytes(postData);
    56 streamWriter.Write(postData);
    57
    58 streamWriter.Close();
    59
    60 request.BeginGetResponse(new AsyncCallback(ResponseCallback), request);
    61 }
    62
    63 private void ResponseCallback(IAsyncResult result)
    64 {
    65 HttpWebRequest request = result.AsyncState as HttpWebRequest;
    66
    67 WebResponse response = null;
    68
    69 try
    70 {
    71 response = request.EndGetResponse(result);
    72 }
    73 catch (Exception ex)
    74 {
    75 _exception = ex.ToString();
    76 }
    77
    78 HttpWebResponse httpResponse = response as HttpWebResponse;
    79
    80 if (httpResponse != null && httpResponse.StatusCode == HttpStatusCode.OK)
    81 {
    82 Stream responseStream = response.GetResponseStream();
    83 using (StreamReader sr = new StreamReader(responseStream))
    84 {
    85 string Text = sr.ReadToEnd();
    86 }
    87 }
    88 else
    89 {
    90 string Text = _exception;
    91 }
    92 }
    93
    94 public void ResponseReady(IAsyncResult asyncResult)
    95 {
    96 HttpWebRequest request = asyncResult.AsyncState as HttpWebRequest;
    97 WebResponse response = request.EndGetResponse(asyncResult) as WebResponse;
    98
    99 var strm = response.GetResponseStream();
    100 var reader = new StreamReader(strm);
    101
    102 string str = reader.ReadToEnd();
    103 reader.Close();
    104 reader.Dispose();
    105 }
    106
    107 public string GetEncodedUri(string srcUri)
    108 {
    109 return HttpUtility.UrlEncode(srcUri);
    110 }
    111
    112 //SHA1
    113 public string SHA1_Hash(string str_sha1_in, string Key)
    114 {
    115
    116 byte[] bytes_sha1_in = UTF8Encoding.UTF8.GetBytes(str_sha1_in);
    117 byte[] bytes_sha1_key = UTF8Encoding.UTF8.GetBytes(Key);
    118 System.Security.Cryptography.HMACSHA1 hmac = new System.Security.Cryptography.HMACSHA1(bytes_sha1_key);
    119
    120 byte[] bytes_sha1_out = hmac.ComputeHash(bytes_sha1_in);
    121 string str_sha1_out = System.Convert.ToBase64String(bytes_sha1_out);
    122
    123 return str_sha1_out;
    124 }
    125 }

    编辑器加载中...

  • 相关阅读:
    JS获取渲染后的样式
    小程序-实现带字母导航的滚动列表
    带排序动画的横向条形图
    VUE模仿百度搜索框,按上下方向键及回车键实现搜索选中效果
    js------科学计数法转换为正常小数
    js------保留指定位数小数
    css布局------左边宽度不定,右边宽度自动填满剩余空间
    css布局------左右宽度固定,中间宽度自适应容器
    css布局------上下高度固定,中间高度自适应容器
    css布局------块元素水平垂直居中的四种方法
  • 原文地址:https://www.cnblogs.com/hebeiDGL/p/2418311.html
Copyright © 2020-2023  润新知