首页 技术 正文
技术 2022年11月18日
0 收藏 575 点赞 3,944 浏览 2733 个字

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://stackoverflow.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://stackoverflow.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!!!

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,492
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,907
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,740
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,494
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,132
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,295