using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
using System.Net.Http; | |
using System.Net; | |
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) + "..."); | |
} | |
} | |
} | |
} |
This comment has been minimized.
This comment has been minimized.
|
This comment has been minimized.
This comment has been minimized.
hi, i am new in programmation. i want to connect a HTTP-server with an webbrowser. so the browser shall send an xml-code to the server. then the server modify it and send back to the browser. i have no idea. can everyone help me pleace? |
This comment has been minimized.
This comment has been minimized.
Thank you very much |
This comment has been minimized.
This comment has been minimized.
Oh yeah. Thanks. |
This comment has been minimized.
This comment has been minimized.
Thanks! |
This comment has been minimized.
This comment has been minimized.
thanks . saved a lot of times |
This comment has been minimized.
This comment has been minimized.
Why not use the .Net built-in BasicAuthenticationHeaderValue (also in the System.Net.Http.Headers namespace)? Like this:
|
This comment has been minimized.
This comment has been minimized.
@dteemull, I can't reference BasicAuthenticationHeaderValue for some reason in my project that uses .net 4.5 |
This comment has been minimized.
This comment has been minimized.
Hi bryanbarnard. I'm working on making something similar to this. I'm trying to write a class which uses httpclient to invoke an endpoint and gets the response. I would really appreciate if you can explain me what does your logic do. So that I can work on mine taking your code as reference. Thank You! |
This comment has been minimized.
This comment has been minimized.
Helped me a lot. Thanks! |
This comment has been minimized.
This comment has been minimized.
Thanks |
This comment has been minimized.
This comment has been minimized.
Gracias por ese pedazo de codigo me salvo la tarde!!! |
This comment has been minimized.
This comment has been minimized.
Thanks! This solution works also on raspberry pi 3 with .net Core 2.0 (preview). This works on Windows, Linux (Ubuntu 16.04-x64) with .net Core 1.1 and 2.0 (preview):
On raspberry pi 3 this throws The solution in this gist runs "everywhere" |
This comment has been minimized.
This comment has been minimized.
Regarding this line:
If I read the standard correctly it is not a requirment that need to be ASCII. So it would be (more) correct to use Do you agree? |
This comment has been minimized.
This comment has been minimized.
Many thanks .Buddy! |
This comment has been minimized.
This comment has been minimized.
@bitbonk That depends on what encoding the server expects. But as long as only ASCII-characters are used in the username/password it will have the same result as Unicode uses the same byte values for all ASCII-characters, good call Unicode consortium. I created a rudimentary helper-class for basic authentication which takes encoding into account for all |
This comment has been minimized.
This comment has been minimized.
hello ders |
This comment has been minimized.
This comment has been minimized.
Many thanks dude! |
This comment has been minimized.
This comment has been minimized.
Good! Any OAuth1 and OAuth2 extension ? |
This comment has been minimized.
This comment has been minimized.
There is a debate whether HttpClient should be wrapped in using block or statically on the app level. Although it implements IDisposable, it seems that by wrapping it in the using block, you can make your app malfunction and get the SocketException. And as Ankit blogs, the performance test results are much in favor of static initialization of the HttpClient. Be sure to read these blog posts as they can help you be more informed about the correct usage of the HttpClient. Wrong usage of the HTTPClient class (in .NET) In .NET, as Simon Timms described in his article, you have to be careful when using the HTTPClient class. https://aspnetmonsters.com/2016/08/2016-08-27-httpclientwrong/ |
This comment has been minimized.
This comment has been minimized.
Hi , I want to call the third party SMS APi call - TransmitSMS from my .net web application that uses framework 4.5. during the call i want to send authorisation , parameter everything. i am new to the programming. can any guys help on this. I already tried using http client: i always get the following error : {StatusCode: 400, ReasonPhrase: 'Bad Request', Version: 1.1, Content: System.Net.Http.StreamContent, Headers: following is the code i wrote for calling
can anybody please help on this. thanks in advance. |
This comment has been minimized.
This comment has been minimized.
Muchas gracias |
This comment has been minimized.
This comment has been minimized.
Very nice! I was able to intercept HttpClient with Fiddler on port 58888! |
This comment has been minimized.
This comment has been minimized.
thanks, men. |
This comment has been minimized.
This comment has been minimized.
God bless you !! |
This comment has been minimized.
This comment has been minimized.
Not sure what gives but for what ever reason creating the task and running it in main with t.start() does nothing. I don't get any errors it just stops when it hits the closing {} of Main(string[] args). The only thing different I added was user input on user name and password and I added an encryption method . So I have the Main(string[] args then I have the following ` public async Task EncodePass(string password)
Then I have }` |
This comment has been minimized.
This comment has been minimized.
THANK YOU MAN! |
This comment has been minimized.
This comment has been minimized.
Thank you very much. |
This comment has been minimized.
This comment has been minimized.
Thank you very much, you saved my day |
This comment has been minimized.
This comment has been minimized.
thankkk youuuu |
This comment has been minimized.
This comment has been minimized.
It took me a while to find out how to encode the username and password into the authorization header, your code saved me a lot of trouble, thanks! |
This comment has been minimized.
Thanks for the code!