C# 开发系列(二)

时间:2023-01-10 10:14:18

1. 参考文档:http://www.yiibai.com/csharp/csharp_environment_setup.html

2. C# ,ASP.NET HTTP Authorization Header

  c参考:

      http://www.makeyuan.com/2014/02/27/1117.html

      http://*.com/questions/4675166/asp-net-http-authorization-header

I would like to know why my asp.net application will not add the header to my post when it is named 'Authorization' but will work fine when I change one character, say "Authorizations". In documentation for other sites they always use the name "Authorization" so I would like to as well and at this point I just want to under stand why.

I have read a few topics about this but have not found any logical reason why.

Here is my code below:

string fileName = "c:\\xyz.xml";
string uri = "http://myserver/Default.aspx";
req = WebRequest.Create(uri);
req.Method = "POST";
req.ContentType = "text/xml";
byte[] authBytes = Encoding.UTF8.GetBytes("DDSServices:jCole2011".ToCharArray());
req.Headers.Add("Authorization", "BASIC " + Convert.ToBase64String(authBytes) );
req.Headers.Add("test", "test");
UTF8Encoding encoder = new UTF8Encoding();
byte[] data = encoder.GetBytes(this.GetTextFromXMLFile(fileName));
req.ContentLength = data.Length;
Stream reqStream = req.GetRequestStream();
reqStream.Write(data, 0, data.Length);
reqStream.Close();
req.Headers.Add("Authorization", "BASIC" + Convert.ToBase64String(authBytes));
System.Net.WebResponse response = req.GetResponse();
System.IO.StreamReader reader = new StreamReader(response.GetResponseStream());
string str = reader.ReadToEnd();

  The other annoying this is when i add the watched variable through fiddler it works fine.

http://*.com/questions/4675166/asp-net-http-authorization-header

3. 官网学习 asp.net mvc4 web应用程序开发。(非常好)

http://www.asp.net/mvc/overview/older-versions/getting-started-with-aspnet-mvc4/intro-to-aspnet-mvc-4

下面是我实现的调用:restful api  的例子:

文件名: testController.cs

C# 开发系列(二)

代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc; using System.IO;
using System.Text;
using System.Net; namespace MvcMovie.Controllers
{
public class testController : Controller
{
//
// GET: /test/ public string Index()
{
// Create the web request
HttpWebRequest request = WebRequest.Create("https://****") as HttpWebRequest; // Add authentication to request
string _auth = string.Format("{0}:{1}", "**username**", "**password**");
string _enc = Convert.ToBase64String(Encoding.ASCII.GetBytes(_auth));
string _cred = string.Format("{0} {1}", "Basic", _enc);
request.Headers[HttpRequestHeader.Authorization] = _cred;
//request.Credentials = new NetworkCredential("diacloud@163.com", "test123456"); // Get response
using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
{
// Get the response stream
StreamReader reader = new StreamReader(response.GetResponseStream()); // Console application output
//Console.WriteLine(reader.ReadToEnd());
return (reader.ReadToEnd());
} return "aaa";
} }
}

  保存,编译后了,浏览器访问(或者 Ctrl+F5):http://localhost:30921/test

C# 开发系列(二)

成功了!!!!ok!!!

C# 开发系列(二)的更多相关文章

  1. WordPress版微信小程序开发系列(二):安装使用问答

    自WordPress版微信小程序发布开源以来,受关注的程度超过我原来的想象.这套程序主要面对的用户是wordpress网站的站长,如果wordpress站想在微信的生态圈得到推广,小程序成为一种重要的 ...

  2. 微信小程序开发系列二:微信小程序的视图设计

    大家如果跟着我第一篇文章 微信小程序开发系列一:微信小程序的申请和开发环境的搭建 一起动手,那么微信小程序的开发环境一定搭好了.效果就是能把该小程序的体验版以二维码的方式发送给其他朋友使用. 这个系列 ...

  3. Windows下USB磁盘开发系列二:枚举系统中所有USB设备

    上篇 <Windows下USB磁盘开发系列一:枚举系统中U盘的盘符>介绍了很简单的获取系统U盘盘符的办法,现在介绍下如何枚举系统中所有USB设备(不光是U盘). 主要调用的API如下: 1 ...

  4. BizTalk开发系列&lpar;二十八&rpar; MSMQ 适配器

    MSMQ(MicroSoft Message Queue,微软消息队列)是在多个不同的应用之间实现相互通信的一种异步传输模式,相互通信的应用可以分布于同一台机器上,也可以分布于相连的网络空间 中的任一 ...

  5. BizTalk开发系列&lpar;二十二&rpar; 开发自定义Map Functoid

    尽管 BizTalk Server 提供许多Functoid以支持一系列不同的操作,但仍可能会遇到需要其他方法的情况.<BizTalk开发系列 Map扩展开发>介绍了通过使用自定义 XSL ...

  6. 【Qt编程】基于Qt的词典开发系列&lt&semi;二&gt&semi;--本地词典的设计

    我设计的词典不仅可以实现在线查单词,而且一个重大特色就是具有丰富的本地词典库:我默认加入了八个类型的词典,如下所示: 由于是本人是通信专业,因此加入了华为通信词典.电子工程词典,又由于我喜爱编程,也加 ...

  7. leaflet-webpack 入门开发系列二加载不同在线地图切换显示(附源码下载)

    前言 leaflet-webpack 入门开发系列环境知识点了解: node 安装包下载webpack 打包管理工具需要依赖 node 环境,所以 node 安装包必须安装,上面链接是官网下载地址 w ...

  8. BizTalk开发系列&lpar;二&rpar; &quot&semi;Hello World&quot&semi; 程序搬运文件

    我们在<QuickLearn BizTalk系列之"Hello World">里讲到了如何快速的开发第一个BizTalk 应用程序.现在我们来讲一下如何把这个程序改成用 ...

  9. BizTalk开发系列&lpar;二十六&rpar; 使用Web Service

    Web Service是在构建SOA平台中广泛使用的技术.在BizTalk开发过程中使用SOAP适配器接收和发送 Web Services 请求.业务流程可以发布为 Web Services 并使用外 ...

  10. BizTalk开发系列&lpar;二十五&rpar; SQL Adapter

    SQL Server 是.NET开发的首选数据库.当然开发BizTalk应用程序很多也离不了SQL Server.针对SQL Server的数据操作BizTalk 提供了SQL Adapter作为与数 ...

随机推荐

  1. Mac Mysql初始密码重置

    今天晚上mac更新后重装MySQL 安装完成后 启动服务,死活登录不了 提示1045错误.按照网上的资料对MySQL密码进行重置.记录步骤如下: 1.首先关闭MySQL服务 系统偏好设置->最下 ...

  2. CodeFirst进行数据迁移之添加字段

    一.为模型更改设置 Code First 数据迁移 1.工具->库程序包管理器->程序包管理器控制台->输入"Enable-Migrations"  或者 Ena ...

  3. angularjs 指令(directive)详解&lpar;2&rpar;

    原文地址 上一篇我们说到了transclude,那么,我们现在继续讲解之后的内容. 9.scope 可选参数,默认值为false.取值: false - 在这个directive里不会创建新的scop ...

  4. expdp导出数据库

    源地址:http://www.cnblogs.com/luluping/archive/2010/03/16/1687093.html 使用EXPDP和IMPDP时应该注意的事项: EXP和IMP是客 ...

  5. awk与sed简明教程

    看到大牛写的关于awk和sed的简明教程,写得很好,为了尊重作者,就不全文转载了,这里标记下链接,方便以后查阅. awk简明教程:http://coolshell.cn/articles/9070.h ...

  6. &lbrack;LeetCode&rsqb;Palindrome Partitioning 找出所有可能的组合回文

    给定一个字符串,切割字符串,这样每个子字符串是一个回文字符串. 要找出所有可能的组合. 办法:暴力搜索+回溯 class Solution { public: int *b,n; vector< ...

  7. CSS3的radial-gradient&lpar;径向渐变&rpar;

    所谓径向渐变,如图下,类似光晕 语法: radial-gradient(  [    [渐变大小]?    [ at 渐变圆心坐标]?  ,]?  颜色[ 开始位置]  [,颜色[ 开始位置]]+); ...

  8. memcache bug

    用memcached有时会报错  the lowest two bytes of the flags array is reserved for pecl/memcache 将以前的 $mc-> ...

  9. CSS-页面滑屏滚动原理

    现在的网站有的时候为了简洁就是很多的单页滑屏滚动介绍,主要呈现方式有两种,一种是整体的元素一直排列下去,假设有五个需要展示的全屏页面,那么高度是500%,只是展示100%,剩下的可以通过transfo ...

  10. Yarn 和 Npm 命令行切换 摘录

    原文作者: @Gant Laborde原文地址: https://shift.infinite.red/np...中文翻译: @文蔺译文地址:http://www.wemlion.com/2016/n ...