//通过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 + "\r\n");
byte[] enterER = Encoding.ASCII.GetBytes("\r\n");

//提交文本字段
if (input != null)
{
string format = "--" + boundary + "\r\nContent-Disposition:form-data;name=\"{0}\"\r\n\r\n{1}\r\n"; //自带项目分隔符
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 + "--\r\n"); //项目最后的分隔符字符串需要带上--
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;
}

HttpWebRequest和WebClient的用法的更多相关文章

  1. HttpWebRequest和WebClient的区别

     HttpWebRequest和WebClient的区别(From Linzheng): 1,HttpWebRequest是个抽象类,所以无法new的,需要调用HttpWebRequest.Creat ...

  2. HttpRequest,WebRequest,HttpWebRequest,WebClient,HttpClient 之间的区别

    HttpRequest,WebRequest,HttpWebRequest,WebClient,HttpClient 今天我们来聊一下他们之间的关系与区别. HttpRequest 类 .NET Fr ...

  3. Linux中用HttpWebRequest或WebClient访问远程https路径

    要想在Linux中用HttpWebRequest或WebClient访问远程https路径,需要作如下处理: 1,更新linux根证书(只需一次,在安装mono或安装jexus独立版后执行) sudo ...

  4. HttpWebRequest、WebClient、RestSharp、HttpClient区别和用途

    HttpWebRequest 已经不推荐直接使用了,这已经作为底层机制,不适合业务代码使用,比如写爬虫的时候WebClient 不想为http细节处理而头疼的coder而生,由于内部已经处理了通用设置 ...

  5. WebRequest/HttpWebRequest/HttpRequest/WebClient/HttpClient的区别

    1.WebRequest和HttpWebRequest WebRequest 的命名空间是: System.Net ,它是HttpWebRequest的抽象父类(还有其他子类如FileWebReque ...

  6. C# HttpWebRequest和WebClient的区别 通过WebClient/HttpWebRequest实现http的post/get方法

    一 HttpWebReques1,HttpWebRequest是个抽象类,所以无法new的,需要调用HttpWebRequest.Create();2,其Method指定了请求类型,这里用的GET,还 ...

  7. C#中HttpWebRequest、WebClient、HttpClient的使用

    HttpWebRequest: 命名空间: System.Net,这是.NET创建者最初开发用于使用HTTP请求的标准类.使用HttpWebRequest可以让开发者控制请求/响应流程的各个方面,如  ...

  8. C#WebClient常见用法

    System.Net.WebClient.DownloadFile(Uri address, String fileName) namespace:System.Net 参数: address:The ...

  9. 【转】C# HttpWebRequest\HttpWebResponse\WebClient发送请求解析json数据

    http://blog.csdn.net/kingcruel/article/details/44036871 版权声明:本文为博主原创文章,未经博主允许不得转载. ================= ...

随机推荐

  1. shell中数组基础语法

    数组的基本赋值 arr=(a b c) arr[index]=a 2.常用的两个方法 str=${arr[@]}(数组转化成字符串) len=${#arr[*]}(数组长度) 3.遍历数组的方法 #! ...

  2. mvc view获取url参数

    视图页面直接获取url get传值, 1.@Request.QueryString["look"] 2.@ViewContext.RequestContext.HttpContex ...

  3. MapReduce 计算模式

    声明:本文摘录自<大数据日知录——架构与算法>一书. 较常见的计算模式有4类,实际应用中大部分ETL任务都可以归结为这些计算模式或者变体. 1.求和模式 a.数值求和 比如我们熟悉的单词计 ...

  4. Git 初始状操作指引

    You have an empty repository To get started you will need to run these commands in your terminal. Ne ...

  5. 【UXPA大赛企业专访】Mockplus:“设计替代开发”将成为现实

    “过去,是‘设计服务于开发’,现在,我认为是‘设计驱动开发’,而在不远的将来,随着AI的落地.大数据和云计算能力的提升,‘设计替代开发’将成为现实.Mockplus也正在为此部署并行动.” 近日,UX ...

  6. oracle两个客户端路径记录

    32 C:\WINDOWS\assembly\GAC_64\Oracle.DataAccess\2.112.3.0__89b483f429c4734264 C:\WINDOWS\Microsoft.N ...

  7. 面向对象与基于对象 学习记录 thread举例

    /********************************************************************/* @file* @author def< qq gr ...

  8. memcached 连接本地问题

    刚开始学memcache ,就遇到一个问题. telnet 127.0.0.1 11211   回车之后就什么都没有提示了.然后不管设置什么都是报error . 表示不知道如何解决!先写个文章记录下来 ...

  9. async 和 await

    win8 app开发中使用async,await可以更方便地进行异步开发. async,await的使用可参考代码:Async Sample: Example from "Asynchron ...

  10. 2018.09.30 bzoj3551:Peaks加强版(dfs序+主席树+倍增+kruskal重构树)

    传送门 一道考察比较全面的题. 这道题又用到了熟悉的kruskal+倍增来查找询问区间的方法. 查到询问的子树之后就可以用dfs序+主席树统计答案了. 代码: #include<bits/std ...