Click here to Skip to main content
15,880,891 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Following is the python script, I have tried WebRequest, Webclient, HttpWebRequest, HttpClient(handler) in C# with GET and POST method too but nothing seems to be working. Sometimes server returns bad request. I have also tried to enable and disable cookie settings through the code in c#. I tried Ironpython, it has different known bug, now trying Pythonnet.

Can anyone guide me what can be the c# equivalent code for that? I shall be thankful.
Python
import requests
import json
import io

headers = {
    'content-type': 'application/json',
    'access_token': 'null',
    'user-agent': 'Mozilla/5.0  AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.',
    'accept': '*/*',
    'referer': 'https://212.112.76.21/details',
    'cookie': '__cfduid=db20875432124; _gl_eu=2.66390154.1603632124; anid=^%^24223%^22; _ta=33222;'
}

params = (
    ('start1', '0'),
    ('ed1', '10')
)

response = requests.get('https://123.125.212.220/details/', headers=headers, params=params)
data = json.loads(response.text)


What I have tried:

I have checked Codeproject and solutions given on stackoverflow. I have tried WebRequest, Webclient, HttpWebRequest, HttpClient(handler) in C# with GET and POST method too but nothing seems to be working. Sometimes server returns bad request. I have also tried to enable and disable cookie settings through the code in c#. I also tried Ironpython, it has different known bug, now trying Pythonnet.
Posted
Updated 23-Nov-20 23:32pm
v2

1 solution

I obviously can't test it, but something like this:
static void Main(string[] args)
{
    HttpClient c = new HttpClient();
    c.DefaultRequestHeaders.Clear();
    c.DefaultRequestHeaders.Add("content-type", "application/json");
    c.DefaultRequestHeaders.Add("user-agent", "Mozilla/5.0  AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86");
    c.DefaultRequestHeaders.Add("accept", "*.*");
    c.DefaultRequestHeaders.Add("referer", "https://212.112.76.21/details");
    c.DefaultRequestHeaders.Add("cookie", "__cfduid=db20875432124; _gl_eu=2.66390154.1603632124; anid=^%^24223%^22; _ta=33222;");
    var result = c.GetAsync($"https://123.125.212.220/details?start1=0&edl=10").Result;
}


But ultimately, I'd install Fiddler which is a tool that intercepts HTTP traffic and shows you the request and response. You can then see exactly what's going on with your Python code, and hopefully exactly what's different with your C#.
 
Share this answer
 
Comments
Programmer0001 24-Nov-20 9:36am    
Hi Rob,

Thanks for your suggestion. I tried it but on reaching:

c.DefaultRequestHeaders.Add("content-type", "application/json");

it is throwing the following error:

System.InvalidOperationException: 'Misused header name. Make sure request headers are used with HttpRequestMessage, response headers with HttpResponseMessage, and content headers with HttpContent objects.'
Rob Philpott 24-Nov-20 9:53am    
Hello. Try replacing the 'Add' with 'TryAddWithoutValidation' and see if that helps? I'm actually more used to using HttpWebRequest for these types of things, so I'm guessing a bit here.

Also, I replaced your */* with *.*. Old habits..
Programmer0001 24-Nov-20 14:13pm    
Yes, it helped. Thank you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900