Вот код в классе формы:
Код: Выделить всё
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace AuthtRestClient
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
#region UI Event Handlers
private void CmdGO_Click(object sender, EventArgs e)
{
RestClient rClient = new RestClient();
rClient.Endpoint = txtRequestURI.Text;
rClient.UserName = txtUserName.Text;
rClient.UserPassword = txtPassword.Text;
DebugOutput("REst Client Created");
string strResponse = string.Empty;
strResponse = rClient.MakeRequest();
DebugOutput(strResponse);
}
#endregion
private void DebugOutput (string strDebugText)
{
try
{
System.Diagnostics.Debug.Write(strDebugText + Environment.NewLine);
txtResponse.Text = txtResponse.Text + strDebugText + Environment.NewLine;
txtResponse.SelectionStart = txtResponse.TextLength;
txtResponse.ScrollToCaret();
}
catch (Exception ex)
{
System.Diagnostics.Debug.Write(ex.Message, ToString() + Environment.NewLine);
}
}
}
}
Код: Выделить всё
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.IO;
namespace AuthtRestClient
{
public enum HttpVerb
{
GET,
POST,
PUT,
DELETE
}
public enum AuthenticationType
{
Basic,
NTLM
}
public enum AuthenticationTechnique
{
RollYourOwn,
NetworkCredential
}
class RestClient
{
public string Endpoint { get; set; }
public string EndPoint { get; }
public HttpVerb HttpMethod { get; set; }
public AuthenticationType AuthType { get; set; }
public AuthenticationTechnique AuthTech { get; set; }
public string UserName { get; set; }
public string UserPassword { get; set; }
public RestClient()
{
EndPoint = string.Empty;
HttpMethod = HttpVerb.GET;
}
public string MakeRequest()
{
string strResponseValue = string.Empty;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Endpoint);
request.Method = HttpMethod.ToString();
string authHeader = System.Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes(UserName + ":" + UserPassword));
request.Headers.Add("Authorization", "Basic " + authHeader);
HttpWebResponse response = null;
try
{
response = (HttpWebResponse)request.GetResponse();
// Process the response stream... (could be JSON, XML, HTML, etc...)
using (Stream responseStream = response.GetResponseStream())
{
if (responseStream != null)
{
using (StreamReader reader = new StreamReader(responseStream))
{
strResponseValue = reader.ReadToEnd();
}// End of StreamReader
}
}// End of using ResponseStream
}
catch(Exception ex)
{
strResponseValue = "{\"errorMessage\":[\"" + ex.Message.ToString() + "\"],\"errors\":{}}";
}
finally
{
if (response != null)
{
((IDisposable)response).Dispose();
}
}
return strResponseValue;
}
}
}
Код: Выделить всё
string authHeader = System.Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes("U4OXU5aHZjYnN4NXJ4bXV6ORIWUp==" +
":" +UserName + ":" + UserPassword));
request.Headers.Add("Authorization", "Basic " + authHeader);
Подробнее здесь: https://stackoverflow.com/questions/495 ... a-rest-api
Мобильная версия