为什么我这webResponse总是有时候会失败
这是代码部分
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Net;
using System.Security.Cryptography;
using System.Text;
namespace Whmcs
{
public class Call
{
private readonly string Username;
private readonly string Password;
private readonly string AccessKey;
private readonly string Url;
private readonly string UrlForSS;
public Call(string Username, string Password, string AccessKey, string Url)
{
this.Username = Username;
this.Password = Password;
this.AccessKey = AccessKey;
this.Url = Url + "/includes/api.php";
this.UrlForclient = Url;
}
private NameValueCollection BuildRequestData(NameValueCollection data)
{
NameValueCollection request = new NameValueCollection()
{
{ "username", Username},
{ "password", Password},
{ "accesskey", AccessKey },
{ "responsetype", "json"}
};
foreach(string key in data)
{
request.Add(key, data[key]);
}
return request;
}
public string MakgeCallFroclient(string urlforclient, NameValueCollection data,bool isUft8)
{
string u = UrlForclient + urlforclient;
if(isUft8)
return Encoding.UTF8.GetString(WebCall(u, data));
else
return Encoding.ASCII.GetString(WebCall(u, data));
}
public string MakeCall(NameValueCollection data)
{
return Encoding.ASCII.GetString(WebCall(Url, data));
}
private byte[] WebCall (string url,NameValueCollection data)
{
byte[] webResponse;
try
{
webResponse = new WebClient().UploadValues(url, BuildRequestData(data));
}
catch (Exception ex)
{
throw new Exception(I18N.GetString("Unable to connect to WHMCS API:"), new Exception(ex.Message.ToString()));
}
return webResponse;
}
private string CalculateMD5Hash(string input)
{
MD5 md5 = MD5.Create();
byte[] inputBytes = Encoding.ASCII.GetBytes(input);
byte[] hash = md5.ComputeHash(inputBytes);
StringBuilder sb = new StringBuilder();
foreach (byte t in hash)
{
sb.Append(t.ToString("x2"));
}
return sb.ToString();
}
}
}
为什么我这webResponse总是有时候会失败