• C# 开发系列(二)


    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

    代码如下:

    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

    成功了!!!!ok!!!

  • 相关阅读:
    简单四则运算实现--第二次作业
    人生第一篇博客
    团队任务1:第一次团队会议
    第二次作业
    自我介绍
    五号团队—团队任务4:每日立会(2018-11-27)
    软件设计与开发准备
    原型设计与UI设计
    第一次团队会议
    课后作业2
  • 原文地址:https://www.cnblogs.com/oxspirt/p/5843370.html
Copyright © 2020-2023  润新知