In this article, I am explaining, How to get response using HttpClient Class.
HttpClient Class
Namespace:System.Net.Http
Assembly:System.Net.Http.dll
using System;
using System.Text;
using System.Threading.Tasks;
using System.Net.Http;
using System.Net;
using System.Web.Script.Serialization;
namespace HTTP_Test
{
class program
{
static void Main()
{
Task t = new Task(HTTP_GET);
t.Start();
Console.ReadLine();
}
static async void HTTP_GET()
{
var TARGETURL = "http://en.wikipedia.org/";
HttpClientHandler handler = new HttpClientHandler()
{
Proxy = new WebProxy("http://127.0.0.1:8888"),
UseProxy = true,
};
Console.WriteLine("GET: + " + TARGETURL);
// ... Use
HttpClient.
HttpClient client = new HttpClient(handler);
var byteArray = Encoding.ASCII.GetBytes("username:password1234");
client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray));
HttpResponseMessage response = await
client.GetAsync(TARGETURL);
HttpContent content = response.Content;
// ... Check
Status Code
Console.WriteLine("Response StatusCode: " + (int)response.StatusCode);
// ... Read the
string.
string result = await
content.ReadAsStringAsync();
// ... Display
the result.
if (result != null
&& result.Length >= 50)
{
Console.WriteLine(result.Substring(0, 50) + "...");
}
}
static void
GetResponseJson(string
sJsonString)
{
string inputJson = (new JavaScriptSerializer()).Serialize(sJsonString);
HttpClient client = new HttpClient();
HttpContent inputContent = new StringContent(inputJson, Encoding.UTF8, "application/json");
var byteArray = Encoding.ASCII.GetBytes("username:password1234");
client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray));
HttpResponseMessage response = client.PostAsync("https://testing.com" + "/GetClient", inputContent).Result;
}
}
}
Ref- https://gist.github.com/bryanbarnard/8102915
No comments:
Post a Comment