HttpWebRequest和WebClient的用法
//通过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的用法的更多相关文章
- HttpWebRequest和WebClient的区别
HttpWebRequest和WebClient的区别(From Linzheng): 1,HttpWebRequest是个抽象类,所以无法new的,需要调用HttpWebRequest.Creat ...
- HttpRequest,WebRequest,HttpWebRequest,WebClient,HttpClient 之间的区别
HttpRequest,WebRequest,HttpWebRequest,WebClient,HttpClient 今天我们来聊一下他们之间的关系与区别. HttpRequest 类 .NET Fr ...
- Linux中用HttpWebRequest或WebClient访问远程https路径
要想在Linux中用HttpWebRequest或WebClient访问远程https路径,需要作如下处理: 1,更新linux根证书(只需一次,在安装mono或安装jexus独立版后执行) sudo ...
- HttpWebRequest、WebClient、RestSharp、HttpClient区别和用途
HttpWebRequest 已经不推荐直接使用了,这已经作为底层机制,不适合业务代码使用,比如写爬虫的时候WebClient 不想为http细节处理而头疼的coder而生,由于内部已经处理了通用设置 ...
- WebRequest/HttpWebRequest/HttpRequest/WebClient/HttpClient的区别
1.WebRequest和HttpWebRequest WebRequest 的命名空间是: System.Net ,它是HttpWebRequest的抽象父类(还有其他子类如FileWebReque ...
- C# HttpWebRequest和WebClient的区别 通过WebClient/HttpWebRequest实现http的post/get方法
一 HttpWebReques1,HttpWebRequest是个抽象类,所以无法new的,需要调用HttpWebRequest.Create();2,其Method指定了请求类型,这里用的GET,还 ...
- C#中HttpWebRequest、WebClient、HttpClient的使用
HttpWebRequest: 命名空间: System.Net,这是.NET创建者最初开发用于使用HTTP请求的标准类.使用HttpWebRequest可以让开发者控制请求/响应流程的各个方面,如 ...
- C#WebClient常见用法
System.Net.WebClient.DownloadFile(Uri address, String fileName) namespace:System.Net 参数: address:The ...
- 【转】C# HttpWebRequest\HttpWebResponse\WebClient发送请求解析json数据
http://blog.csdn.net/kingcruel/article/details/44036871 版权声明:本文为博主原创文章,未经博主允许不得转载. ================= ...
随机推荐
- [leetcode]721. Accounts Merge账户合并
Given a list accounts, each element accounts[i] is a list of strings, where the first element accoun ...
- 11-st跳舞消耗体力最少
/* 炫舞家 ST 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 ...
- 9-最短路径(dijkstra)
参考博客:http://www.wutianqi.com/?p=1890 #include <iostream>using namespace std;#define max 1< ...
- js 获取高度
网页可见区域宽 :document.body.clientWidth; 网页可见区域高:document.body.clientHeight; 网页可见区域高:document.body.offs ...
- CFR - another java decompiler批量反编译jar文件
jd-gui众所周知,业界公认的反编译必备工具. 笔者目前遇到一个java项目,社区版,想做一个本地化的版本,询问官方,官方说闭源,无奈之下只能反编译了. 面对那么多jar,jd-gui一个个去反编译 ...
- abp 的坑
多数据库连接问题 viewmodel的验证问题 发布后,一段查找sln查找代码无法适用生产环境问题 多语言问题,默认中文设置与模板配置文件不统一
- 重新学习pytorch的库函数等..
http://blog.csdn.net/victoriaw/article/list/10 开始第一篇: http://blog.csdn.net/VictoriaW/article/details ...
- HTML5 APP应用实现图片上传及拍照上传功能
https://blog.csdn.net/zmzwll1314/article/details/46965663 http://www.cnblogs.com/leo0705/ https://zh ...
- 2018.08.31 bzoj1419 Red is good(期望dp)
描述 桌面上有R张红牌和B张黑牌,随机打乱顺序后放在桌面上,开始一张一张地翻牌,翻到红牌得到1美元,黑牌则付 出1美元.可以随时停止翻牌,在最优策略下平均能得到多少钱. 输入 一行输入两个数R,B,其 ...
- React 组件的生命周期API和事件处理
一.简单记录React的组件的简洁的生命周期API: A:实例化期: 一个实例第一次被创建时所调用的API与其它后续实例被创建时所调用的API略有不同. 实例第一次被创建时会调用getDefaultP ...