//通過web方式,從遠端伺服器端下載檔案:
public static void DownLoad(string Url, string FileName, string machinetype)
{
try
{
if (Url.Contains(“http”))
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
HttpWebResponse webResponse = (HttpWebResponse)request.GetResponse();
System.Drawing.Image downImage = System.Drawing.Image.FromStream(request.GetResponse().GetResponseStream());
string imageUrl = ConfigurationManager.AppSettings[“imageUrl”];
string dertory = Path.Combine(imageUrl, machinetype);
string fileName = string.Format(“{0}.png”, FileName);
if (!System.IO.Directory.Exists(dertory))
{
System.IO.Directory.CreateDirectory(dertory);
}
downImage.Save(dertory + “\” + fileName);
downImage.Dispose();
}
else
{
return;
}
}
catch (Exception ex)
{
DataAccess.WriteLog(ex.Message + “: Url: ” + Url + “, systemtwoid: ” + FileName + “, CFTname: ” + machinetype + ” ” + ex.StackTrace, “DownLoad”);
return;
}
}
///WebClient
public static string PostMobileApi(string fun, string token, string data)
{
string TYCSR_url = ConfigurationManager.AppSettings[“TYCSR”].ToString();
WebClient client = new WebClient();
client.Headers.Add(“Content-Type”, “application/x-www-form-urlencoded”);
client.Encoding = Encoding.UTF8;
//string url = “http://localhost:8084/api/ApiIndex/main”;//
string postData = “fun=” + fun + “&token=5D993541B0E4BC9193CA17CDF191E6C1&vst=2&way=2&ver=1.0&data=” + data;
string result = client.UploadString(TYCSR_url, “POST”, postData);
return result;
}
//HttpWebRequest post請求資料
public static string PostFormData(string password, string username)
{
string rst = “”;
Dictionary<string, string> input = new Dictionary<string, string>();
input.Add(“client_id”, “” + ConfigurationSettings.AppSettings[“client_id”].ToString() + “”);
input.Add(“client_secret”, “” + ConfigurationSettings.AppSettings[“client_secret”].ToString() + “”);
input.Add(“grant_type”, “” + ConfigurationSettings.AppSettings[“grant_type”].ToString() + “”);
input.Add(“password”, “” + password + “”);
input.Add(“scope”, “” + ConfigurationSettings.AppSettings[“scope”].ToString() + “”);
input.Add(“username”, “” + username + “”);
input.Add(“oauth_nonce”, “9”);
input.Add(“sysId”, “1”);
string oAuthUrl = ConfigurationSettings.AppSettings[“oauthUrl”].ToString();
string url = oAuthUrl + “connect/token”;
string boundary = “—————————-” + DateTime.Now.Ticks.ToString(“x”);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.ContentType = “multipart/form-data; boundary=” + boundary;
request.Method = “POST”;
request.KeepAlive = true;
request.Expect = “”;
MemoryStream stream = new MemoryStream();
byte[] line = Encoding.ASCII.GetBytes(“–” + boundary + ”
“);
byte[] enterER = Encoding.ASCII.GetBytes(”
“);
//提交文字欄位
if (input != null)
{
string format = “–” + boundary + ”
Content-Disposition:form-data;name=”{0}”
{1}
“; //自帶專案分隔符
foreach (string key in input.Keys)
{
string s = string.Format(format, key, input[key]);
byte[] data = Encoding.UTF8.GetBytes(s);
stream.Write(data, 0, data.Length);
}
}
byte[] foot_data = Encoding.UTF8.GetBytes(“–” + boundary + “–
“); //專案最後的分隔符字串需要帶上–
stream.Write(foot_data, 0, foot_data.Length);
request.ContentLength = stream.Length;
Stream requestStream = request.GetRequestStream(); //寫入請求資料
stream.Position = 0L;
stream.WriteTo(requestStream);
stream.Close();
requestStream.Close();
try
{
HttpWebResponse response;
try
{
response = (HttpWebResponse)request.GetResponse();
try
{
using (var responseStream = response.GetResponseStream())
using (var mstream = new MemoryStream())
{
StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8, true, 100);
rst = @”[” + reader.ReadToEnd() + “]”;
reader.Close();
response.Close();
}
}
catch (Exception ex)
{
throw ex;
}
}
catch (WebException ex)
{
throw ex;
}
}
catch (Exception ex)
{
throw ex;
}
//解析資料,返回token值
List<GetPersonAccounts> jobInfoList = JsonConvert.DeserializeObject<List<GetPersonAccounts>>(rst);
GetPersonAccounts personAccounts = new GetPersonAccounts();
foreach (GetPersonAccounts perAccount in jobInfoList)
{
personAccounts.access_token = perAccount.access_token;
}
return personAccounts.access_token;
}