Getting some invalid response(some random characters) in c# - c#

I am doing a get request in c# but i am getting some invalid response than the original content
code
using System;
using System.Net;
using System.Text;
using System.IO;
public class Test
{
// Specify the URL to receive the request.
public static void Main(string[] args)
{
HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create("http://www1.bloomingdales.com/api/store/v2/stores/367,363,6113,364,4946?upcNumber=849262004629");
// Set some reasonable limits on resources used by this request
myHttpWebRequest.MaximumAutomaticRedirections = 4;
myHttpWebRequest.MaximumResponseHeadersLength = 4;
// Set credentials to use for this request.
myHttpWebRequest.Credentials = CredentialCache.DefaultCredentials;
myHttpWebRequest.Method = "GET";
myHttpWebRequest.AllowAutoRedirect = false;
myHttpWebRequest.ContentLength = 0;
myHttpWebRequest.Accept = "application/json, text/javascript, */*; q=0.01";
myHttpWebRequest.Headers.Add("Cookie", "ShippingCountry=US;");
myHttpWebRequest.UserAgent = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/49.0.2623.108 Chrome/49.0.2623.108 Safari/537.36";
myHttpWebRequest.Headers.Add("Accept-Encoding", "gzip, deflate, sdch");
myHttpWebRequest.Headers.Add("Accept-Language", "en-US,en;q=0.8");
myHttpWebRequest.Headers.Add("X-Macys-ClientId", "NavApp");
var response = (HttpWebResponse)myHttpWebRequest.GetResponse();
var rmyResponseHeaders = response.Headers;
Console.WriteLine("Content length is {0}", response.ContentLength);
Console.WriteLine("Content type is {0}", response.ContentType);
// Get the stream associated with the response.
Stream receiveStream = response.GetResponseStream();
// Pipes the stream to a higher level stream reader with the required encoding format.
StreamReader readStream = new StreamReader(receiveStream, Encoding.UTF8);
Console.WriteLine("Response stream received.");
Console.WriteLine(readStream.ReadToEnd());
Console.ReadLine();
response.Close();
readStream.Close();
}
}
The above getting invalid character like ???????? and some other characters
You can check original response here
curl 'http://www1.bloomingdales.com/api/store/v2/stores/367,363,6113,364,4946?upcNumber=849262004629' -H 'Cookie:shippingCountry=US;' -H 'Accept-Encoding: gzip, deflate, sdch' -H 'User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/49.0.2623.108 Chrome/49.0.2623.108 Safari/537.36' -H 'Accept-Language: en-US,en;q=0.8' -H 'Accept: application/json, text/javascript, */*; q=0.01' -H 'X-Macys-ClientId: NavApp' --compressed
How to fix this and get exact response(orginal response)?

You're not decompressing the response stream. Set the Accept-Encoding header to empty string until you're ready to work out which compression was used and decompress it.

Related

unable to reproduce curl postrequest with c# and httpClient

so I have this website
After inspecting the network traffic for the download button I got the below curl post request
curl "https://flood-map-for-planning.service.gov.uk/pdf" -X POST -H "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:104.0) Gecko/20100101 Firefox/104.0" -H "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8" -H "Accept-Language: en-US,en;q=0.5" -H "Accept-Encoding: gzip, deflate, br" -H "Content-Type: application/x-www-form-urlencoded" -H "Origin: https://flood-map-for-planning.service.gov.uk" -H "Connection: keep-alive" -H "Referer: https://flood-map-for-planning.service.gov.uk/flood-zone-results?easting=429240&northing=431613&location=LS118TR" -H "Upgrade-Insecure-Requests: 1" -H "Sec-Fetch-Dest: document" -H "Sec-Fetch-Mode: navigate" -H "Sec-Fetch-Site: same-origin" -H "Sec-Fetch-User: ?1" -H "TE: trailers" --data-raw "id=1660136366038&polygon=&center="%"5B429240"%"2C431613"%"5D&reference=&scale=2500"
I went over to this website in order to convert the curl to c#
This is what I got
using (var httpClient = new HttpClient())
{
using (var request = new HttpRequestMessage(new HttpMethod("POST"), "https://flood-map-for-planning.service.gov.uk/pdf"))
{
request.Headers.TryAddWithoutValidation("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:104.0) Gecko/20100101 Firefox/104.0");
request.Headers.TryAddWithoutValidation("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8");
request.Headers.TryAddWithoutValidation("Accept-Language", "en-US,en;q=0.5");
request.Headers.TryAddWithoutValidation("Accept-Encoding", "gzip, deflate, br");
request.Headers.TryAddWithoutValidation("Origin", "https://flood-map-for-planning.service.gov.uk");
request.Headers.TryAddWithoutValidation("Connection", "keep-alive");
request.Headers.TryAddWithoutValidation("Referer", "https://flood-map-for-planning.service.gov.uk/flood-zone-results?easting=429240&northing=431613&location=LS118TR");
request.Headers.TryAddWithoutValidation("Upgrade-Insecure-Requests", "1");
request.Headers.TryAddWithoutValidation("Sec-Fetch-Dest", "document");
request.Headers.TryAddWithoutValidation("Sec-Fetch-Mode", "navigate");
request.Headers.TryAddWithoutValidation("Sec-Fetch-Site", "same-origin");
request.Headers.TryAddWithoutValidation("Sec-Fetch-User", "?1");
request.Headers.TryAddWithoutValidation("TE", "trailers");
request.Content = new StringContent("id=1660136366038&polygon=&center=");
request.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/x-www-form-urlencoded");
var response = await httpClient.SendAsync(request);
}
}
I changed it to:
var httpClient = new HttpClient();
var request =
new HttpRequestMessage(new HttpMethod("POST"), "https://flood-map-for-planning.service.gov.uk/pdf");
request.Headers.TryAddWithoutValidation("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:104.0) Gecko/20100101 Firefox/104.0");
request.Headers.TryAddWithoutValidation("Accept", "application/pdf");
request.Headers.TryAddWithoutValidation("Accept-Language", "en-US,en;q=0.5");
request.Headers.TryAddWithoutValidation("Accept-Encoding", "gzip, deflate, br");
request.Headers.TryAddWithoutValidation("Origin", "https://flood-map-for-planning.service.gov.uk");
request.Headers.TryAddWithoutValidation("Connection", "keep-alive");
request.Headers.TryAddWithoutValidation("Referer", "https://flood-map-for-planning.service.gov.uk/flood-zone-results?easting=429240&northing=431613&location=LS118TR");
request.Headers.TryAddWithoutValidation("Upgrade-Insecure-Requests", "1");
request.Headers.TryAddWithoutValidation("Sec-Fetch-Dest", "document");
request.Headers.TryAddWithoutValidation("Sec-Fetch-Mode", "navigate");
request.Headers.TryAddWithoutValidation("Sec-Fetch-Site", "same-origin");
request.Headers.TryAddWithoutValidation("Sec-Fetch-User", "?1");
request.Headers.TryAddWithoutValidation("TE", "trailers");
request.Content = new StringContent("center=&scale=2500");
var response = httpClient.Send(request);
response.Content.Headers.Add("Content-Disposition", "inline;filename=\"Testpdf.pdf\"");
response.Content.Headers.Add("Content-Name", "Testpdf.PDF");
response.Content.Headers.Add("Content-Type", "application/pdf;charset=UTF-8");
if (response.IsSuccessStatusCode)
{
using (FileStream fs = new FileStream("somepdf.pdf", FileMode.CreateNew))
{
using (StreamWriter writer = new StreamWriter(fs))
{
var contentStream = response.Content.ReadAsStream(); // get the actual content stream
writer.Write(contentStream);
}
}
}
This is the issue.
My goal is to download the pdf locally.
I usually get a file which is 1KB or 6KB.
The curl command with an output parameter works without an issue. I'm just not sure what the above c# http post request is missing.
As you can see I've added the filestream and streamwriter usages.
I've also tried to play with the response in order to nagivate it to an application/pdf response.
Any ideas why I am doing wrong?
=======================================================
EDIT
Thanks to #thehennyy,
here is the working solution:
var unixTimestamp = (long)DateTime.UtcNow.Subtract(DateTime.UnixEpoch).TotalSeconds;
HttpClientHandler handler = new HttpClientHandler()
{
AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate
};
using (var httpClient = new HttpClient(handler))
{
using (var request =
new HttpRequestMessage(new HttpMethod("POST"), "https://flood-map-for-planning.service.gov.uk/pdf"))
{
request.Headers.TryAddWithoutValidation("Referer",
"https://flood-map-for-planning.service.gov.uk/flood-zone-results?easting=429240&northing=431613&location=LS118TR");
request.Content =
new StringContent($"id={unixTimestamp}&polygon=&center=[429240,431613]&reference=&scale=2500");
request.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/x-www-form-urlencoded");
var response = await httpClient.SendAsync(request);
if (response.IsSuccessStatusCode)
{
using (FileStream fs = new FileStream("somepdf.pdf", FileMode.Create))
{
var contentStream = await response.Content.ReadAsStreamAsync();
await contentStream.CopyToAsync(fs);
}
}
}
}
There are a few things to consider here:
It seems like the curl to httpclient converter had a problem converting the post content. The following works for me:
request.Content = new StringContent("id=1&polygon=&center=[429240,431613]&reference=&scale=2500");
request.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/x-www-form-urlencoded");
The parameter id has to be provided, otherwise the request will fail. The website uses the current unix timestamp as value for the id parameter.
Adding headers to the response response.Content.Headers.Add([...]) is not meaningful, just delete these lines.
Writing the content to disk can be done simpler:
using (FileStream fs = new FileStream("somepdf.pdf", FileMode.Create))
{
var contentStream = await response.Content.ReadAsStreamAsync();
await contentStream.CopyToAsync(fs);
}
While testing i got the same "wrong" files, these are usual just html responses, sometimes containing an error message. View them as html. Maybe they seem like gibberish, then you have to turn on automatic decompression:
HttpClientHandler handler = new HttpClientHandler()
{
AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate
};
var httpClient = new HttpClient(handler);
The automatic decompression values should match this headers values:
request.Headers.TryAddWithoutValidation("Accept-Encoding", "gzip, deflate");
Current versions of dotnet also support "br" - DecompressionMethods.Brotli.
Using automatic decompression is helpful in nearly every case.

C# HttpWebRequest send xhr request - 400 bad request

I have to send ajax request from C#. In browser the request looks like:
Request URL:https://sts-service.mycompany.com/UPNFromUserName
Request method:POST
Remote address:xxxx
Status code:
200
Version:HTTP/2.0
Referrer Policy:strict-origin-when-cross-origin
Headers:
Host: sts-service.mycompany.com
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:68.0) Gecko/20100101 Firefox/68.0
Accept: */*
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate, br
Referer: https://sts.mycompany.com/
Content-type: application/x-www-form-urlencoded
Content-Length: 17
Origin: https://sts.mycompany.com
DNT: 1
Connection: keep-alive
Pragma: no-cache
Cache-Control: no-cache
Params:
Form data:
MS.Aution
Cookies: no cookie
And in C# my request:
WebRequest webRequest = WebRequest.Create("https://sts-service.mycompany.com/UPNFromUserName");
((HttpWebRequest)webRequest).Referer = "https://sts.mycompany.com/";
((HttpWebRequest)webRequest).Host = "sts-service.mycompany.com";
((HttpWebRequest)webRequest).KeepAlive = true;
((HttpWebRequest)webRequest).AllowAutoRedirect = true;
((HttpWebRequest)webRequest).UseDefaultCredentials = true;
((HttpWebRequest)webRequest).UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:68.0) Gecko/20100101 Firefox/68.0";
webRequest.ContentType = "application/x-www-form-urlencoded";
((HttpWebRequest)webRequest).Accept = "*/*";
((HttpWebRequest)webRequest).Headers.Add("Origin", "https://sts.mycompany.com");
((HttpWebRequest)webRequest).Headers.Add("Accept-Encoding", "gzip, deflate, br");
((HttpWebRequest)webRequest).Headers.Add("Accept-Language", "en-US,en;q=0.5");
((HttpWebRequest)webRequest).Headers.Add("Upgrade-Insecure-Requests", #"1");
((HttpWebRequest)webRequest).Headers.Add("DNT", #"1");
((HttpWebRequest)webRequest).AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
webRequest.Method = HttpRequestType.POST.ToString();
string msg = "MSCnE.Automation";
webRequest.ContentLength = msg.Length;
Stream reqStream = webRequest.GetRequestStream();
byte[] msgb = System.Text.Encoding.UTF8.GetBytes(msg);
reqStream.Write(msgb, 0, msgb.Length);
reqStream.Close();
var response = (HttpWebResponse)webRequest.GetResponse();
StreamReader sr = new StreamReader(response.GetResponseStream());
string Result = sr.ReadToEnd();
response.Close();
I get error:
The remote server returned an error: (400) Bad Request.
In browser in Network tab the request looks like:
Type is json but in Headers Content Type is application/x-www-form-urlencoded
Maybe this is the reason?
something along these lines (not tested):
public async Task<string> SendPOST()
{
var dict = new Dictionary<string, string>();
dict.Add("DNT", "1");
dict.Add("someformdata","MSCnE.Automation");
using (var formdata = new System.Net.Http.FormUrlEncodedContent(dict))
{
//do not use using HttpClient() - https://learn.microsoft.com/en-us/dotnet/api/system.net.http.httpclient
using (System.Net.Http.HttpClient httpClient = new System.Net.Http.HttpClient())
{
formdata.Headers.ContentType = System.Net.Http.Headers.MediaTypeHeaderValue.Parse("application/x-www-form-urlencoded");
formdata.Headers.ContentType.CharSet = "UTF-8";
httpClient.DefaultRequestHeaders.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:68.0) Gecko/20100101 Firefox/68.0");
using (var response = await httpClient.PostAsync("https://sts-service.mycompany.com/UPNFromUserName", formdata))
{
if (response.IsSuccessStatusCode)
{
var postresult = await response.Content.ReadAsStringAsync();
return postresult;
}
else
{
string errorresult = await response.Content.ReadAsStringAsync();
return errorresult;
}
}
}
}
}

Why I can't get request with HttpWebRequest?

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

Not getting the correct encoding to extract body from HttpWebResponse

I am trying to extract data from a government site which is rendered on display of a pop up. I checked the network console and got the POST request URL and able to replicate the request-response on Postman. Now I am trying to make the call programmatically. I tried using the default code generated by Postman but it did not work.
I am writing the code in C# and I am able to get the response but I am not able to get the correct encoding to extract the response.
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://saubhagya.gov.in/dashboard/data/dashboard_saubhagya");
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.Accept = "application/json, text/javascript, */*; q=0.01";
request.Host = "saubhagya.gov.in";
request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36";
request.Headers.Add("X-Requested-With", "XMLHttpRequest");
request.Headers.Add("Accept-Encoding: gzip, deflate");
request.Headers.Add("Accept-Language: en-US,en;q=0.9");
request.Headers.Add("Cache-Control: no-cache");
request.Headers.Add("Pragma: no-cache");
request.Headers.Add("Origin: http://saubhagya.gov.in");
request.Referer = "http://saubhagya.gov.in/";
String CookieStr = "_ga=GA1.3.590075225.1533991967; _gid=GA1.3.790472263.1533991967; saubhagyasession=BHxX%2FFfttUfxM7JhoIruGVzdq0m%2F4sGeTn95c%2BUB%2BGvJok9PkS3g9pR8vLVfeEJ1XB8UULGNThvbAeN5HfAu%2FE6qt%2F5X3qL8Yla4my0qmxSmz6Q9ztpLztCD0PyY17uWDnJgkSjSt%2BSF0B5Xh32SUsxBXHH%2BeFGwtIXdAnzSLcxC0MO8KZSiE2io4ksZO6AZ31YSxnGei6CluQzg4fCFgXvVwR4%2F00%2FKAbf0MnhLwaTtXxD0jngmDv3Rjy8enD87c20vwObHGTgcLC3KQoh2lw5L1WRF1lVLlpjzLrUoeJV3cD8o0c15bT5SA%2FV1Y8OqFPhqhpr0%2BzzG%2FbAVs6OKMmLiokl7hHrPx5NECDsmY3KzmCkNHka%2B1ueEWTv%2FTOUqH2hll2A8485gFhqFgnrh%2FKkhOb6I8lChI2QQoyHr%2B9U%3D92add88ce105d8b3ec1dd72efa1dd7ec9b9f1e52";
CookieContainer cookiecontainer = new CookieContainer();
string[] cookies = CookieStr.Split(';');
foreach (string cookie in cookies)
cookiecontainer.SetCookies(new Uri("http://saubhagya.gov.in/dashboard/data/dashboard_saubhagya"), cookie);
request.CookieContainer = cookiecontainer;
using (var streamWriter = new StreamWriter(request.GetRequestStream()))
{
string json = "ci_csrf_token=&state=35&district=638&village=645115&vtype=&discom=&search_text=&uuid=&maptype=states&kyroargs=&drilldownkey=&kyroclickid=&kyrorefreshid=&page=dashboard_saubhagya";
streamWriter.Write(json);
streamWriter.Flush();
streamWriter.Close();
}
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
using (StreamReader sr = new StreamReader(response.GetResponseStream(), System.Text.Encoding.GetEncoding(response.CharacterSet)))
{
var result = sr.ReadToEnd();
}
I get an encoded/junk string as output.
Requesting for help!
In the header you state that you accept gzip, but the response is not decompressed on your end, so just add:
request.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip;
Then you can remove this line because headers will be added automatically:
//request.Headers.Add("Accept-Encoding: gzip, deflate");

httpwebrequest failing in c#

I am doing a web request for an url using c#
this is the original curl request
curl 'http://www1.bloomingdales.com/api/store/v2/stores/367,363,6113,364,4946?upcNumber=808593890516' -H 'Cookie:shippingCountry=US;' -H 'Accept-Encoding: gzip, deflate, sdch' -H 'User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/49.0.2623.108 Chrome/49.0.2623.108 Safari/537.36' -H 'Accept-Language: en-US,en;q=0.8' -H 'Accept: application/json, text/javascript, */*; q=0.01' --compressed
I wrote a c# code for this
public string variations_curl ()
{
var request = (HttpWebRequest)WebRequest.Create("http://www1.bloomingdales.com/api/store/v2/stores/367,363,6113,364,4946?upcNumber=5045493369123");
//request.Referer = "http://www.youtube.com/"; // optional
request.Method = WebRequestMethods.Http.Post;
//request.Headers["origin"] = "http://www1.bloomingdales.com";
request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
request.Headers["Accept-Language"] = "en-US,en;q=0.8";
request.UserAgent = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/49.0.2623.108 Chrome/49.0.2623.108 Safari/537.36";
//request.ContentType = "application/x-www-form-urlencoded";
request.Headers ["Cookie"] = "shippingCountry=US;";
request.Accept = "application/json";
var response = (HttpWebResponse)request.GetResponse();
using (var reader = new StreamReader(response.GetResponseStream()))
{
var html = reader.ReadToEnd();
return html;
}
}
But the above code throws some error
An unhandled exception of type 'System.Net.WebException' occurred in System.dll
Additional information: The remote server returned an error: (411) Length Required.
How to fix this error and make the program work?
Just add to request methode
request.ContentLength = 0;
This worked for me

Categories

Resources