亲爱的朋友,
我有一点问题,当我尝试运行我的程序时显示错误401(未经授权)
我需要输入用户名:admin和密码:admin到Json,但我不知道如何把它
有没有人可以帮助我?这是我的脚本
public partial class Form1:Form { private static void PostJson(string uri ,template postParameters) { string postData = JsonConvert.SerializeObject(postParameters); byte [] bytes = Encoding.UTF8.GetBytes(postData); var httpWebRequest =(HttpWebRequest)WebRequest.Create(uri); httpWebRequest.Method =" POST" ;; httpWebRequest.ContentLength = bytes.Length; httpWebRequest.ContentType =" application / json;字符集= UTF-8英寸; using(Stream requestStream = httpWebRequest.GetRequestStream()) { requestStream.Write(bytes,0,bytes.Count()); } var httpWebResponse =(HttpWebResponse)httpWebRequest.GetResponse(); if(httpWebResponse.StatusCode!= HttpStatusCode.OK) { string message = String.Format(" POST failed。Received HTTP {0}",httpWebResponse.StatusCode); 抛出新的ApplicationException(message); } } public Form1() { InitializeComponent(); } private void button1_Click(object sender,EventArgs e) { PostJson(" http:// xxxxxxxxxxx / CustomerSalesOrder",new template {}) ;var template = new template { CardCode =" C001", PostingDate =" 2017-05-17", DocDueDate =" 2017-05-17", TaxDate =" 2017-05-17", SalesPersonCode =" -1" ;, DocumentNumberingPOS =" xx", PrimaryNumberingPOS =" xxx",}; template.Lines.Add(new template_item { ItemCode =" ITEM0001", WarehouseCode =" PST-000", Quantity =" 1", Price =" 50000",}); template.Lines.Add(new template_item { ItemCode =" ITEM0002", WarehouseCode =" PST-000",数量=" 1", Price =" 10000" }); } } 公共类模板 { public string CardCode {get;组; } public string PostingDate {get;组; } 公共字符串DocDueDate {get;组; } 公共字符串TaxDate {get;组; } 公共字符串SalesPersonCode {get;组; } public string DocumentNumberingPOS {get;组; } public string PrimaryNumberingPOS {get;组; } 公共静态列表< template_item> Lines = new List< template_item>(); } public class template_item { public string ItemCode {get;组; } 公共字符串WarehouseCode {get;组; } public string Quantity {get;组; } public string Price {get;组; } }
感谢您的帮助
问候,
Steve Henry
解决方案
不幸的是,这完全取决于您正在呼叫的网址。如果它是REST API,那么您必须使用API的auth方案。大多数人使用OAuth,但有4种不同的类型。对于客户端凭据的常见情况,您首先必须调用 a(通常是单独的)URL进行身份验证,该身份验证需要客户端ID,密码和oauth范围。您将获得一个持有者令牌,然后将其添加到HTTP标头以调用实际的API。
如果URL是ASP.NET表单auth应用程序,那么您需要先转到登录的URL并将UN / PWD作为表单数据主体传递。您将获得带有身份验证信息的cookie。然后,您需要将cookie附加到后续请求中。您可能也需要包含会话信息。
请参阅您要调用的URL的文档,以确定他们希望身份验证的工作方式。您不太可能在每次请求时通过UN / PWD。这是不安全和低效的。
Michael Taylor www.michaeltaylorp3
dear friend,
I have a little problem, when I try to run my program it show an error 401 (unauthorized)
I need to input username: admin and password: admin into Json, but I don't how to put it
does anyone could help me ? this is my script
public partial class Form1 : Form { private static void PostJson(string uri, template postParameters) { string postData = JsonConvert.SerializeObject(postParameters); byte[] bytes = Encoding.UTF8.GetBytes(postData); var httpWebRequest = (HttpWebRequest)WebRequest.Create(uri); httpWebRequest.Method = "POST"; httpWebRequest.ContentLength = bytes.Length; httpWebRequest.ContentType = "application/json; charset=utf-8"; using (Stream requestStream = httpWebRequest.GetRequestStream()) { requestStream.Write(bytes, 0, bytes.Count()); } var httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse(); if (httpWebResponse.StatusCode != HttpStatusCode.OK) { string message = String.Format("POST failed. Received HTTP {0}", httpWebResponse.StatusCode); throw new ApplicationException(message); } } public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { PostJson("xxxxxxxxxxx/CustomerSalesOrder", new template{ });var template = new template { CardCode = "C001", PostingDate = "2017-05-17", DocDueDate = "2017-05-17", TaxDate = "2017-05-17", SalesPersonCode = "-1", DocumentNumberingPOS = "xx", PrimaryNumberingPOS = "xxx", }; template.Lines.Add(new template_item { ItemCode = "ITEM0001", WarehouseCode = "PST-000", Quantity = "1", Price = "50000", }); template.Lines.Add(new template_item { ItemCode = "ITEM0002", WarehouseCode = "PST-000", Quantity = "1", Price = "10000" }); } } public class template { public string CardCode { get; set; } public string PostingDate { get; set; } public string DocDueDate { get; set; } public string TaxDate { get; set; } public string SalesPersonCode { get; set; } public string DocumentNumberingPOS { get; set; } public string PrimaryNumberingPOS { get; set; } public static List<template_item> Lines = new List<template_item>(); } public class template_item { public string ItemCode { get; set; } public string WarehouseCode { get; set; } public string Quantity { get; set; } public string Price { get; set; } }
Thanks for your help
Regards,
Steve Henry
解决方案Unfortunately that is completely dependent upon the URL you're calling. If it is a REST API then you have to use the auth scheme for the API. Most use OAuth but there are 4 different types. For the common case of client credentials you first have to call a (generally separate) URL for authentication that requires the client ID, secret and oauth scope(s). You get back a bearer token that you then add to the HTTP header for calls to the actual API.
If the URL is an ASP.NET forms auth app then you need to first go to the URL for the login and pass the UN/PWD as the form data body. You get back a cookie with the auth information. You then need to attach the cookie to subsequent requests. You probably also need to include session information.
Refer to the documentation for the URL you're calling to determine how they expect authentication to work. It is unlikely you would pass UN/PWD on each request. That is unsecure and inefficient.
Michael Taylor www.michaeltaylorp3
更多推荐
在C#中添加用户名和密码Json文件
发布评论