git_bsmd/nsw/Dänemark/SafeSeaNetTest/Program.cs
2016-10-25 06:23:37 +00:00

160 lines
5.7 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Net;
using System.IO;
using System.Net.Http;
namespace SafeSeaNetTest
{
class Program
{
private static string veryBasicUri = "https://nswtestapi.safeseanet.dk/";
private static string baseUri = "https://nswtestapi.safeseanet.dk/api/v1/";
private static string searchUri = "search?query=contentType: ship&take=5";
static void Main(string[] args)
{
Login3();
Console.Read();
}
private static async void Login3()
{
var credential = new NetworkCredential("bsm-schick", "R1g048r20l", "SSNTEST");
var myCache = new CredentialCache();
Uri uri = new Uri(Path.Combine(baseUri, searchUri));
Uri authUri = new Uri(uri.GetLeftPart(UriPartial.Authority));
// welche Uri wird hier tatsächlich hinzugefügt? nur die Base oder ganz speziell?
myCache.Add(authUri, "ntlm", credential);
var handler = new HttpClientHandler();
handler.AllowAutoRedirect = true;
handler.Credentials = myCache;
// Create an HttpClient with the handler object
HttpClient httpClient = new HttpClient(handler);
// Wait to get the reponse, and you can use the reponse in your code
HttpResponseMessage responseMessage = await httpClient.GetAsync(uri);
string jsonMessage;
using (Stream responseStream = await responseMessage.Content.ReadAsStreamAsync())
{
jsonMessage = new StreamReader(responseStream).ReadToEnd();
}
}
private static async void Login()
{
NetworkCredential credential = new NetworkCredential("bsm-schick", "R1g048r20l");
/*
WebRequest req = WebRequest.Create(baseUri);
req.AuthenticationLevel = System.Net.Security.AuthenticationLevel.MutualAuthRequested;
req.Credentials = credential;
WebResponse resp = req.GetResponse();
StreamReader reader = new StreamReader(resp.GetResponseStream());
var token = reader.ReadToEnd().Trim();
Console.WriteLine(token);
*/
// Variante 2
// Create a network credential with username, password, and damain name
// var credential = new NetworkCredential("username", "password", "domainname");
//var myCache = new CredentialCache();
// Add the target Uri to the CredentialCache with credential object
//myCache.Add(new Uri(baseUri), "NTLM", credential);
// Create an HttpClientHandler to add some settings
var handler = new HttpClientHandler();
// handler.AllowAutoRedirect = true;
handler.Credentials = credential;
// Create an HttpClient with the handler object
HttpClient httpClient = new HttpClient(handler);
// Wait to get the reponse, and you can use the reponse in your code
string querystring = Path.Combine(baseUri, searchUri);
HttpResponseMessage responseMessage = await httpClient.GetAsync(querystring);
string jsonMessage;
using (Stream responseStream = await responseMessage.Content.ReadAsStreamAsync())
{
jsonMessage = new StreamReader(responseStream).ReadToEnd();
}
Console.WriteLine(jsonMessage);
}
private static async void Login2()
{
NetworkCredential credential = new NetworkCredential("bsm-schick", "R1g048r20l", "SSNTEST");
//CredentialCache cc = new CredentialCache();
//cc.Add(new Uri(baseUri), "NTLM", credential);
HttpClientHandler handler = new HttpClientHandler();
handler.Credentials = credential;
handler.AllowAutoRedirect = true;
//handler.Credentials = cc;
string querystring = Path.Combine(baseUri, searchUri);
//var encoded = Convert.ToBase64String(Encoding.ASCII.GetBytes("bsm-schick:Melder2016"));
handler.PreAuthenticate = true;
Uri uri = new Uri(querystring);
HttpRequestMessage hrm = new HttpRequestMessage(HttpMethod.Get, uri);
HttpClient httpClient = new HttpClient(handler);
httpClient.DefaultRequestHeaders.Accept.Clear();
httpClient.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
//httpClient.DefaultRequestHeaders.Add("Content-Type", "application/json");
//httpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", encoded);
Console.WriteLine("Query: " + querystring);
HttpResponseMessage responseMessage = await httpClient.GetAsync(querystring);
if(responseMessage.StatusCode == HttpStatusCode.Unauthorized)
{
// do NTLM auth
// hrm.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("NTLM")
}
string jsonMessage;
using (Stream responseStream = await responseMessage.Content.ReadAsStreamAsync())
{
jsonMessage = new StreamReader(responseStream).ReadToEnd();
}
Console.WriteLine(jsonMessage);
}
}
}