Rest URL consumption guidance required - c#

I have a rest url provided by client , It looks something like www.baseurl/api// I couldn't get result if I try to consume it from console application getting error 503.
However I m able to browse it from my browser and it returns the proper json of Entity Details. Please help me with this.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp15
{
public class Class1
{
private const string URL = "www.baseurl/api/<EntityName>/<EntityID>";
static void Main(string[] args)
{
Class1.CreateObject();
}
private static void CreateObject()
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL);
request.Method = "GET";
request.ContentType = "application/json";
try
{
WebResponse webResponse = request.GetResponse();
using (Stream webStream = webResponse.GetResponseStream() ?? Stream.Null)
using (StreamReader responseReader = new StreamReader(webStream))
{
string response = responseReader.ReadToEnd();
Console.Out.WriteLine(response);
}
}
catch (Exception e)
{
Console.Out.WriteLine("-----------------");
Console.Out.WriteLine(e.Message);
}
}
}
}

Related

Odata Connected service - How to use it?

I am currently trying to make a dummy console app, to understand how AX web API works.
I added my odata webservice as a connected service, and from there not sure how i reference it in my main or make any get, calls to the service.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using Microsoft.Data.OData;
using System.IO;
using System.Xml;
namespace ConsoleApp3
{
class Program
{
static void Main(string[] args)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://10.51.32.14:8101/DynamicsAx/Services/ODataQueryService/Invent");
request.Method = "GET";
request.ContentType = "application/x-www-form-urlencoded";
//request.UseDefaultCredentials = true;
//request.PreAuthenticate = true;
request.Credentials = new NetworkCredential("admin_nc_keer", "C^Reup-rK)EH1j");
//request.Credentials = CredentialCache.DefaultCredentials;
try
{
using (WebResponse response = request.GetResponse())
{
using (Stream stream = response.GetResponseStream())
{
XmlTextReader reader = new XmlTextReader(stream);
Console.WriteLine(" {0}={1}", reader.Name, reader.Value);
}
}
}
catch(WebException e)
{
if (e.Status == WebExceptionStatus.Timeout)
{
Console.WriteLine("Timeout!");
}
else throw;
}
Console.WriteLine("End!");
Console.ReadLine();
}
}
}
Which returns nothing...
There is no container týpe defined for the odata data, but an contenttype, being
<content type="application/xml">

Authorisation Header and Content Type in C#

I am writing a code in C# to build the console to implement get request.
We were using POSTMAN to achieve the task but we decided to build our own.
I need to pass the user name and pw in headers of the request.
Since i am new to programming,can you guide me.
I have written below code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net.Http;
namespace ConsoleApp2
{
class Program
{
private static object response;
static void Main(string[] args)
{
GetRequest("https://www.google.com");
Console.ReadKey();
}
async static void GetRequest(string url)
{
using (HttpClient client = new HttpClient())
{
using (HttpResponseMessage response = await client.GetAsync(url))
{
using (HttpContent content = response.Content)
{
string mycontent = await content.ReadAsStringAsync();
Console.WriteLine(mycontent);
}
}
}
}
}
}
You can try following code snippet
HttpClient client = new HttpClient()
var byteArray = Encoding.ASCII.GetBytes("username:password1234");
client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray));
DefaultRequestHeaders is not working, I have used below snippet for authentication
string _ContentType = "application/json";
httpWebRequest.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(_ContentType));
var _CredentialBase64 = "xxxxxxxxx=";
httpWebRequest.DefaultRequestHeaders.Add("Authorization", String.Format("Basic {0}", _CredentialBase64));

How to make cURL call in C#

How can I make the following cURL call via C# console app.
curl -k https://serverName/clearprofile.ashx -H "Host: example.com"
I have tried CurlSharp. Unfortunately I could not build successfully this project because of missing libcurlshim64 assembly.
From here I understood that the best approach is by using HttpClient class, but I don`t know how exactly to make the above mentioned cURL call from my console application.
If you just want something simple I would use the HttpClient class built into the .Net Framework like this:
using System.Net.Http;
using System.Threading.Tasks;
namespace ScrapCSConsole
{
class Program
{
static void Main(string[] args)
{
HttpClient client = new HttpClient();
client.DefaultRequestHeaders.Host = "example.com";
Task<string> task = client.GetStringAsync("https://serverName/clearprofile.ashx");
task.Wait();
Console.WriteLine(task.Result);
}
}
}
For more complex stuff, you can also use the HttpWebRequest class like this:
using System;
using System.IO;
using System.Net;
namespace ScrapCSConsole
{
class Program
{
static void Main(string[] args)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://google.co.uk");
request.Host = "example.com";
HttpStatusCode code;
string responseBody = String.Empty;
try
{
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
code = response.StatusCode;
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
responseBody = reader.ReadToEnd();
}
}
}
catch (WebException webEx)
{
using (HttpWebResponse response = (HttpWebResponse)webEx.Response)
{
code = response.StatusCode;
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
responseBody = reader.ReadToEnd();
}
}
}
Console.WriteLine($"Status: {code}");
Console.WriteLine(responseBody);
}
}
}

Creating a login session to a webpage

I hope I ask in the correct way in here as it is my first in stackoverflow.
I am pretty new in C# and WP8, but I am working on a small project where I through my WP8 app login to my page and then my wish is to be able to, after the login, to somehow use the session/cookie from the login to navigate in a WebBrowser control through the other "protected" pages.
I have indeed searched the forums and the net, but I have not found the specific answer elsewhere.
Below I have my login session which works and "result" gives me the HTML of the page after login. But then I am a bit stuck...
Maybe there is a better/smarter/easier way?
Best Regards
Martin
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using HTTPPost.Resources;
using System.IO;
using System.Text;
using System.Diagnostics;
using System.Text.RegularExpressions;
namespace HTTPPost
{
public partial class MainPage : PhoneApplicationPage
{
public MainPage()
{
InitializeComponent();
Loaded += new RoutedEventHandler(MainPage_Loaded);
}
void MainPage_Loaded(object sender, RoutedEventArgs e)
{
System.Uri myUri = new System.Uri("http://homepage.com/index.php");
HttpWebRequest myRequest = (HttpWebRequest)HttpWebRequest.Create(myUri);
myRequest.Method = "POST";
myRequest.ContentType = "application/x-www-form-urlencoded";
myRequest.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback), myRequest);
}
void GetRequestStreamCallback(IAsyncResult callbackResult)
{
HttpWebRequest myRequest = (HttpWebRequest)callbackResult.AsyncState;
// End the stream request operation
Stream postStream = myRequest.EndGetRequestStream(callbackResult);
// Create the post data
string postData = "user=usernamepass=password";
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
// Add the post data to the web request
postStream.Write(byteArray, 0, byteArray.Length);
postStream.Close();
// Start the web request
myRequest.BeginGetResponse(new AsyncCallback(GetResponsetStreamCallback), myRequest);
}
void GetResponsetStreamCallback(IAsyncResult callbackResult)
{
HttpWebRequest request = (HttpWebRequest)callbackResult.AsyncState;
HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(callbackResult);
using (StreamReader httpWebStreamReader = new StreamReader(response.GetResponseStream()))
{
string result;
result = httpWebStreamReader.ReadToEnd();
MiniBrowser.NavigateToString(result);
Debug.WriteLine(result);
}
}
}
}
Well, there is lots of answers about getting and storing cookies, but I have a trick to avoid using them. The trick is to use same instance of WebClient for all requests on this page after login sequence. See my code:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
namespace SomeApp
{
public class WebRequests
{
//Making property of HttpClient
private static HttpClient _client;
public static HttpClient Client
{
get { return _client; }
set { _client = value; }
}
//method to download string from page
public static async Task<string> LoadPageAsync(string p)
{
if (Client == null)// that means we need to login to page
{
Client = await Login(Client);
}
return await Client.GetStringAsync(p);
}
// method for logging in
public static async Task<HttpClient> Login(HttpClient client)
{
client = new HttpClient();
var content = new FormUrlEncodedContent(new[]
{
new KeyValuePair<string, string>("email", "someone#example.com"),
new KeyValuePair<string, string>("password", "SoMePasSwOrD")
});
var response = await client.PostAsync("https://www.website.com/login.php", content);
return client;
}
var page1Html = await LoadPageAsync("https://www.website.com/page1.php");
}
}

Call to OData service from C#

I use the code below to call an OData service (which is the working service from Odata.org) from C# and I don't get any result.The error is in the response.GetResponseStream().
Here is the error :
Length = 'stream.Length' threw an exception of type 'System.NotSupportedException'
I want to call to the service and parse the data from it, what is the simpliest way to do that?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Net;
using System.IO;
using System.Xml;
namespace ConsoleApplication1
{
public class Class1
{
static void Main(string[] args)
{
Class1.CreateObject();
}
private const string URL = "http://services.odata.org/OData/OData.svc/Products?$format=atom";
private static void CreateObject()
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL);
request.Method = "GET";
request.ContentType = "application/xml";
request.Accept = "application/xml";
using (WebResponse response = request.GetResponse())
{
using (Stream stream = response.GetResponseStream())
{
XmlTextReader reader = new XmlTextReader(stream);
}
}
}
}
}
I ran your code on my machine, and it executed fine, I was able to traverse all XML elements retrieved by the XmlTextReader.
var request = (HttpWebRequest)WebRequest.Create(URL);
request.Method = "GET";
request.ContentType = "application/xml";
request.Accept = "application/xml";
using (var response = request.GetResponse())
{
using (var stream = response.GetResponseStream())
{
var reader = new XmlTextReader(stream);
while (reader.Read())
{
Console.WriteLine(reader.Value);
}
}
}
But as #qujck suggested, take a look at HttpClient. It's much easier to use.
If you're running .NET 4.5 then take a look at HttpClient (MSDN)
HttpClient client = new HttpClient();
client.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage response = await client.GetAsync(endpoint);
Stream stream = await response
.Content.ReadAsStreamAsync().ConfigureAwait(false);
response.EnsureSuccessStatusCode();
See here and here for complete examples

Categories

Resources