I'm trying to get a image using HttpClient and I'm getting this error:
HttpRequestException: An error occurred while sending the request
Using WebClient with DownloadData method, works fine.
var cookieContainer = new CookieContainer();
using (var handler = new HttpClientHandler() { CookieContainer = cookieContainer })
using (var client = new HttpClient(handler))
{
client.DefaultRequestHeaders.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36");
client.DefaultRequestHeaders.Add("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8");
client.DefaultRequestHeaders.Add("Accept-Encoding", "gzip, deflate");
client.DefaultRequestHeaders.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36");
client.DefaultRequestHeaders.Add("Accept-Language", "pt-BR,pt;q=0.8,en-US;q=0.6,en;q=0.4");
client.DefaultRequestHeaders.Add("Upgrade-Insecure-Requests", "1");
client.GetAsync("cookieGenerateUrl").Wait();
client.DefaultRequestHeaders.Remove("Accept");
client.DefaultRequestHeaders.Add("Accept", "image/webp,image/apng,image/*,*/*;q=0.8");
var imagem = client.GetByteArrayAsync(imageUrl).Result;
}
What is the equivalent to WebClient.DownloadData on HttpClient class?
Probably the problem here is that you are calling client.GetByteArrayAsync with no proper handling of the awaitable.
the fix is:
var res = await client.GetByteArrayAsync(url);
or
byte[] arr;
client.GetByteArrayAsync(url).ContinueWith((x) => arr = x.Result);
Related
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => 'http://vbypass.tech/bottele/',
CURLOPT_USERAGENT => 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36 ',
CURLOPT_POST => 1,
CURLOPT_SSL_VERIFYPEER => false, //
CURLOPT_POSTFIELDS => http_build_query(array(
'apptoken' => $basic,
'md' => $md,
'mdm' => $mdm,
You can do it with RestSharp and Newtonsoft libraries..
using RestSharp;
using Newtonsoft.Json;
var client = new RestClient("http://vbypass.tech");
client.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36";
var request = new RestRequest("/bottele", Method.POST);
var post = new { apptoken = "YOURAPPTOKEN", md = "MD", mdm = "MDM" };
request.AddJsonBody(JsonConvert.SerializeObject(post));
var response = client.Execute(request);
if (response.StatusCode == System.Net.HttpStatusCode.OK)
{
Console.WriteLine(response.Content);
}
var post = new { apptoken = "YOURAPPTOKEN", md = "MD", mdm = "MDM" };
fill api token values and enjoy !
So I always try to see any site cookies but i get empty string
var cookieJar = new CookieContainer();
var client = new RestClient("https://server.com") {
UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64)
AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36",
};
client.CookieContainer = cookieJar;
var request = new RestRequest(Method.GET);
var cookie = client.CookieContainer.GetCookieHeader(new
Uri("https://server.com"));
MessageBox.Show("" + cookie);
Site Cookies
You haven't sent any request to get cookie. Add Execute method call to your code
var request = new RestRequest(Method.GET);
client.Execute(request); //add this line
var cookie = client.CookieContainer.GetCookieHeader(new Uri("https://server.com"));
I'd like to get phone number from https://sprzedajemy.pl/doskonale-dla-pary-planujacej-poszerzenie-rodziny-sprawdz-warszawa-2-1b8e55-nr57347155
Phone number is "protect" and I have to click "show number" to get request with phone. Before I send request I have to get from source data-id="805c74a74f3ea9fe6db5da90d722" from button "show number" and send POST with this token as _rp_offerID.
Correct answer is:
<span><strong> 516 000 551</strong></span>
My answer is:
?
My complete code:
HttpWebRequest getRequest = (HttpWebRequest)WebRequest.Create("https://sprzedajemy.pl/oferta-dane.telefon");
getRequest.Method = "POST";
getRequest.UserAgent = "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.116 Safari/537.36";
getRequest.ContentType = "application/x-www-form-urlencoded";
getRequest.Host = "sprzedajemy.pl";
getRequest.Referer = url;
getRequest.Headers.Add("accept-encoding", "gzip, deflate, br");
getRequest.Headers.Add("accept-language", "pl,en-US;q=0.9,en;q=0.8,ru;q=0.7");
getRequest.Headers.Add("origin", "https://sprzedajemy.pl");
getRequest.Headers.Add("X-Requested-With", "XMLHttpRequest");
var postData = "_rp_offerID=" + itemId;
var data = Encoding.ASCII.GetBytes(postData);
getRequest.ContentLength = data.Length;
using (var stream = getRequest.GetRequestStream())
{
stream.Write(data, 0, data.Length);
}
var httpResponseP = (HttpWebResponse)getRequest.GetResponse();
var streamReaderP = new StreamReader(httpResponseP.GetResponseStream());
string strPhone = streamReaderP.ReadToEnd();
Console.WriteLine(strPhone);
I don't know what is wrong with my code...
If I use REST client for Chrome with:
POST https://sprzedajemy.pl/oferta-dane.telefon
User-Agent: Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.116 Safari/537.36
Content-Type: application/x-www-form-urlencoded
Referer: http://sprzedajemy.pl/doskonale-dla-pary-planujacej-poszerzenie-rodziny-sprawdz-warszawa-2-1b8e55-nr57347155
accept-encoding: gzip, deflate, br
accept-language: pl,en-US;q=0.9,en;q=0.8,ru;q=0.7
origin: https://sprzedajemy.pl
X-Requested-With: XMLHttpRequest
Host: sprzedajemy.pl
Content-Length: 48
Body Form Data:
_rp_offerID=80e158b0281e04a2102fd7bce6eba0cd3833
Answer is correct
Why don't you use HttpClient ?
it's much easier!
check the example below:
using System;
using System.Net.Http;
using System.Text;
namespace httpClient
{
class Program
{
static void Main(string[] args)
{
using (var client = new HttpClient() {BaseAddress = new Uri("https://sprzedajemy.pl")})
{
client.DefaultRequestHeaders.Add("accept-encoding", "gzip, deflate, br");
client.DefaultRequestHeaders.Add("accept-language", "pl,en-US;q=0.9,en;q=0.8,ru;q=0.7");
client.DefaultRequestHeaders.Add("origin", "https://sprzedajemy.pl");
client.DefaultRequestHeaders.Add("X-Requested-With", "XMLHttpRequest");
var postData = "_rp_offerID=80e158b0281e04a2102fd7bce6eba0cd3833";
var stringContent = new StringContent(postData, Encoding.Default, "application/x-www-form-urlencoded");
var result = client.PostAsync("oferta-dane.telefon", stringContent).GetAwaiter().GetResult();
}
}
}
}
i`ve tested this code and the return was 200
I try to allow users to select images from the users google drive.
I follow this doc: https://developers.google.com/picker/docs/.
When i recive the image url from google ('/drive/v2/files/' + FileId) then i try to download tile image in my .NET code:
var WebRequest = (HttpWebRequest)System.Net.WebRequest.Create(url);
WebRequest.AllowWriteStreamBuffering = true;
WebRequest.Timeout = 30000;
WebRequest.KeepAlive = false;
WebRequest.UserAgent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36";
WebRequest.AllowAutoRedirect = true;
WebRequest.Headers.Add("Authorization", "Bearer " + token);
byte[] Arr;
using (var WebResponse = WebRequest.GetResponse())
{
using (var Stream = WebResponse.GetResponseStream())
{
Arr = Stream.StreamToByteArray();
}
WebResponse.Close();
}
return Arr;
But all i get is a empty (white) webpage. With the url i get from google, i can se the image in my browser, but i cant download it in my code.
How can i get the image downloaded?
Use instead WebClient:
client = new WebClient();
client.Headers.Add("User-Agent", "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36");
client.Headers.Add("Authorization", "Bearer " + token);
client.AllowWriteStreamBuffering = true;
return client.DownloadData(url); /// DownloadData returns a byte array
I'm trying to log in to https://secure.domaintools.com/log-in/?logout (http://whois.domaintools.com)
using (var myWebClient = new WebClient())
{
string loginData = "username=test#test.com&passowrd=pwd";
string response = myWebClient.UploadString("https://secure.domaintools.com/log-in/?logout", "POST", loginData);
myWebClient.Headers["User-Agent"] = "MOZILLA/5.0 (WINDOWS NT 6.1; WOW64) APPLEWEBKIT/537.1 (KHTML, LIKE GECKO) CHROME/21.0.1180.75 SAFARI/537.1";
myWebClient.DownloadString("http://whois.domaintools.com");
doc.LoadHtml(page);
}
But have this error
Error 417 Expectation failed
How can I fix that?
Add this property to your code
ServicePointManager.Expect100Continue = false;