UnicodeEncoding HashAlgorithm CryptoConfig BitConverter

using System.Text;
using System.Security.Cryptography;

public static string Encrypt(string password)
  {
   Byte[] clearBytes=new UnicodeEncoding().GetBytes(password);
   Byte[] hashedBytes=((HashAlgorithm)CryptoConfig.CreateFromName("MD5")).ComputeHash(clearBytes);
   return BitConverter.ToString(hashedBytes);
  } 

下面是对你提供的代码进行优化的建议: ```csharp internal async Task<ResponseResult> PostDataAsync(string url, string param) { Uri requestUri = new Uri(BaseLocalHost + url); ResponseResult httpResponseBody = new ResponseResult(); try { HttpStringContent content = new HttpStringContent(param, Windows.Storage.Streams.UnicodeEncoding.Utf8, "application/json"); HttpResponseMessage httpResponseMessage = await httpClient.PostAsync(requestUri, content); httpResponseMessage.EnsureSuccessStatusCode(); string res1 = await httpResponseMessage.Content.ReadAsStringAsync(); httpResponseBody = ConvertToResponseResult(res1); await new MessageDialog(httpResponseBody.message.ToString(), "响应体").ShowAsync(); return httpResponseBody; } catch (Exception ex) { string res = "Error:" + ex.HResult.ToString("X") + "Message:" + ex.Message; await new MessageDialog(res, "网络请求失败").ShowAsync(); return httpResponseBody; } } private ResponseResult ConvertToResponseResult(string json) { var serializer = new DataContractJsonSerializer(typeof(ResponseResult)); using (var ms = new MemoryStream(Encoding.UTF8.GetBytes(json))) { return serializer.ReadObject(ms) as ResponseResult; } } ``` 优化说明: 1. 将参数类型由 `String` 改为 `string`,与 C# 的命名约定一致。 2. 将参数的初始化放在使用之前,避免不必要的初始化。 3. 将数据格式转换的代码抽取到一个单独的方法 `ConvertToResponseResult` 中,提高代码的可读性和复用性。 4. 使用 `using` 语句来确保资源的正确释放。 希望以上优化建议对你有帮助!如果你还有其他问题,请随时提问。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值