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 版权声明:本文为博主原创文章,未经博主允许不得转载. ================= ...
随机推荐
- 解读超轻量级DI容器-Guice与Spring框架的区别【转载】
依赖注入,DI(Dependency Injection),它的作用自然不必多说,提及DI容器,例如spring,picoContainer,EJB容器等等,近日,google诞生了更轻巧的DI容器… ...
- Linux-——grep
概念介绍 grep (global search regular expression(RE) and print out the line,全面搜索正则表达式并把行打印出来)是一种强大的文本搜索工具 ...
- JTemplate学习(四)
注释.自定方法.模板嵌套子模板.循环输出不同class <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "htt ...
- java 同步
本文主要记录java进行同步的方案及锁优化的方法,来自<深入理解jvm> 定义 线程安全:多线程访问一个对象时,不用考虑这些线程在运行时环境下的调度与交替执行,也不需要额外的同步或调用方进 ...
- Laravel 处理 Options 请求的原理以及批处理方案
0. 背景 在前后端分离的应用中,需要使用CORS完成跨域访问.在CORS中发送非简单请求时,前端会发一个请求方式为OPTIONS的预请求,前端只有收到服务器对这个OPTIONS请求的正确响应,才会发 ...
- Laravel 开启跨域请求
项目中用到了接口,外部调用的时候老是请求不到,本地请求却没问题,查了下说是因为跨域的问题.根据网上所说解决方法如下: 1.建立中间件Cors.php命令:php artisan make:middle ...
- CentOs7中的网卡配置工具
CentOs7中的网卡配置工具 摘自:https://blog.51cto.com/13572810/2087991 misslaziness1人评论2715人阅读2018-03-17 22:09:1 ...
- 一篇文章Tornado快速入门
Tornado是一个PythonWeb框架.一个异步网络库.通过使用非阻塞网络I/O,Tornado能够处理数以千计的连接,这意味着对于实时Web服务来说,Tornado是一个理想的Web框架. 作为 ...
- msys2 git status显示中文文件名问题
git config [--global] core.quotepath off https://stackoverflow.com/questions/5854967/git-msysgit-acc ...
- 8.19 extjs jar 包使用。
一.文件结构在ExtJS官网下载好4.0的开发包解压后,我们得到如图的文件结构 文件/文件夹名作用builds 压缩后的ExtJS代码,体积更小,更快docs 开发文档examples 官方演示 ...