Python urllib with proxy not working while C# works - c#

I am trying to get data from an API using python 'urllib.request'. My requests sometime need to post json data.
My network is behind a proxy
When i try to get the data using C# code, everything works great:
WebClient wc = new WebClient();
WebProxy wp = new WebProxy("{IP}", 8080);
wc.Proxy = wp;
var request = "https://{API address and params}";
Uri serviceUri = new Uri(request);
string download = wc.DownloadString(serviceUri);
My python code is:
import urllib
address = "https://{API address and params}"
req = urllib.request.Request(address)
req.set_proxy("{IP}:8080", "http")
response = urllib.request.urlopen(req)
My python code throws a 400 error code exception - 'bad request'
What am i doing wrong?

Related

403 Forbidden error while invoking API in c# code

This API is working through postman.
I am trying the same thrid-party API from my application like below:
string requestUrl = string.Empty;
string result = string.Empty;
System.Net.WebClient client = new System.Net.WebClient();
using (client)
{
requestUrl = "https://api.fyndx1.de/hogwarts/aggregators/api/v1/config1/authToken";
client.QueryString.Add("username", "tester");
client.QueryString.Add("password", "pwd123");
result = client.DownloadString(requestUrl);
}
403 error is coming. I tried to add User agent to header parameters after querystring but no use.
client.Headers.Add("User-Agent: Other");
Any help is appreciated. Thanks.

HttpClient authorization error on linux machine

This is my simple spike code:
var url = "http://url.de";
var username = "user";
var password = "password";
var client = new HttpClient();
var base64 = Convert.ToBase64String(Encoding.ASCII.GetBytes(username + ":" + password));
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", base64);
var result = client.GetAsync(url).Result;
On my Ubuntu server I always get a 401 authentication error with this code.
When I fire the webservice data on the same machine with Postman, the call works.
If I run the code directly on the webservice server (Windows Server) itself, the call works too.
What can be the problem?
Firewall is disabled.
I found the problem.
For whatever reason, the call via the code only works over HTTPS.

Error message when posting to php from C#

I want to post variables from C# to a php-script on my webserver. I tried this following code from the internet but it returns this error message:
Error: The remote server returned an error. (406) Not Acceptable.
The c# part:
string URI = "https://myserver.com/post.php";
string myParameters = "param1=value1&param2=value2";
using (WebClient wc = new WebClient())
{
wc.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
string HtmlResult = wc.UploadString(URI, myParameters);
MessageBox.Show(HtmlResult);
}
The php part:
<?php
if(isset($_POST['param1']) && isset($_POST['param2']))
{
$user = $_POST['param1'];
$date = $_POST['param2'];
echo $user . ' : ' . $date;
}
?>
I have tested it with a test post server and it works but it won't work on my server.
Your backend service is saying that the response type it is returning is not provided in the Accept HTTP header in your Client request.
Source: What is "406-Not Acceptable Response" in HTTP?

Not found error when using WebClient to send a GET request

I'm trying to send GET request to my server, using WebClient and get error for "Not found". In C++ the same request works fine. My URL looks like:
"https://www.example.com/something/something&param1=data1&param2={}"
... and the request look like
WebClient client = new WebClient();
string res = client.DownloadString(url);
What am I doing wrong?
I succeeded! i was missing the headers.
my URL looks like "https://www.example.com/something/something?param1=data1&param2={}"
WebClient client = new WebClient();
client.Headers.Add("Content-Type", "application/json");
client.Headers.Add("Accept-Version", "4");
client.Headers.Add("User-Agent", "v4.1.0.0");
string res = client.DownloadString(url);
Thanks for the help

How to send request and get response from localhost in c#

I have installed "vivekn sentimental tool" which is running in my local host.when we send an http request using unirest post method to the server which contains a string it sends response whether it is positive or negative.I had used it in java.but anyone tell me how to use that in c#
here is my java code
HttpResponse request = Unirest.post("http:localhost:1681/api/text/")
.header("X-Mashape-Authorization", "xxxxxxxxxx")
.field("txt", "i like icecream").asJson();
JSONObject js = new JSONObject(request.getBody().toString());
Object ob = js.get("result");
JSONObject job = new JSONObject(ob.ToString);
String sentiment = (String)job.get("sentiment");
what I need is I want to send a request to the localhost and receive a response which is json with or without using unirest
It's going to roughly look something like this:
using (var wc = new WebClient())
{
wc.Headers.Add(
"X-Mashape-Authorization", "1R1tvmPFLFL7brrvfO9jvsqnvhiVggAn");
var body = wc.DownloadString("http:localhost:1681/api/text/");
var js = Newtonsoft.Json.Linq.JObject.Parse(body);
/* do something with the json */
}
You'll need to get the Newtonsoft library from NuGet - ID: "Newtonsoft.Json".

Categories

Resources