I am trying to consume a REST API and i have an issue that it is driving me crazy...
I created a dll to wrap the service consumption layer, and i found that if i consume the services using the c# interactive feature it works fine.
The issue is when i try to consume it from another DLL, it is throwing Bad Request exception when executing GetResponse()
The code executed is...
var url = $"{_salesForceInstance}/services/data/{_salesForceVersion}/query/?q=SELECT+Id,Name,AccountId+from+Contact+WHERE+Email+=+'{email}'";
var webRequest = (HttpWebRequest)WebRequest.Create(url);
webRequest.ServicePoint.CloseConnectionGroup(webRequest.ConnectionGroupName);
webRequest.Method = "GET";
webRequest.ContentType = "application/json";
webRequest.Headers.Add("Authorization", $"Bearer {_authorizationToken}");
var webResponse = webRequest.GetResponse() as HttpWebResponse;
I also when debugging code, to send the same through POSTMAN and it works fine...
Any ideas??? I am quite frustrated at this point why it works when consuming the dll from C# interactive but not from another dll...
As spender suggested i tried with Fiddler...
I found that despite of the fact both request looks the same, (the one from c# interactive and the one from the console app), in the one that returned the HTTP 400 error code the response included the following message
"[{"message":"TLS 1.0 has been disabled in this organization. Please use TLS 1.1 or higher when connecting to Salesforce using https.","errorCode":"UNSUPPORTED_CLIENT"}]"
That message does not appear when debugging...
Finally i solved by including the line described below in my code in order to use TLS 1.2
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
Now it works !!!
Hope this helps to someone else!
Thanks!
Related
HttpRequest request = new HttpRequest();
request.AddHeader("Host", "api.dwebsir.com");
request.Proxy = ProxyClient.Parse(ProxyType.Http, proxy);
this way of adding custom host header doesnt seems to work.
When i ignore the the proxy and while posting without headers also seems to work.
But when i postdata with proxy and request.Header it is not taking and i am not getting any response
But when i postdata with proxy and i am receiving this The requested URL /as/token.oauth2 was not found on this server.
i am pretty sure the error is with Host as headers i am not getting the proper fix
There is a way to do this, as Explanation given here:
http://blogs.msdn.com/feroze_daud/archive/2005/03/31/404328.aspx
next version of the framework (.NET Framework 4.0) will make it easier.
http://blogs.msdn.com/ncl/archive/2009/07/20/new-ncl-features-in-net-4-0-beta-2.aspx
Hope this helps you.
I'm creating a Xamarin.Droid app with Visual Studio (C#).
I have a RESTful web service (WebAPI) from which I want to retrieve some data in a Xamarin Droid project, using HttpWebRequest. The fact that it's WebAPI and HttpWebRequest is not the key as I had the exact same problem (described below) with some older WSE 3.0 web services and the use of HttpClient in the generated proxies. In the case of those WSE 3.0 web services we solved the problem by extending the generated proxy class to use ModernHttpClient instead. We didn't realize this problem manifests outside of HttpClient. I now think we must be doing something wrong, as opposed to blaming HttpClient.
In the current iteration, connecting to WebAPI, I'm using HttpWebRequest. Whether using HttpWebRequest or HttpClient, everything works fine as long as I'm connecting via http. As soon as I try to connect via https the request fails. For clarification, the web server hosting the web service does have a valid certificate (it's not expired, and has a valid CA.)
I have not been able to find any solution to this problem, despite a ton of Googling. I can barely find references to other people having this problem, much less a solution, which makes me think there must be something wrong in what I'm doing.
Basically, you can this method to a Xamarin.Forms Droid project and call it. If url is using https then request.GetResponse() throws a WebException: "Error: SecureChannelFailure (The authentication or decryption has failed.)". If url is using plain http then the call executes cleanly and returns the expected data.
using System;
using System.IO;
using System.Net;
using Android.App;
using Android.Content.PM;
using Android.OS;
// ...
private string FetchData()
{
// Public test server that's handy for testing JSON code.
// http://jsonplaceholder.typicode.com
string url = "https://jsonplaceholder.typicode.com/posts/1";
// Create an HTTP web request using the URL:
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Timeout = 10 * 1000; // 10 seconds
request.Accept = "application/json";
request.Method = "GET";
string result = "";
try
{
using (WebResponse response = request.GetResponse())
{
result = "Presumably, do something with response.GetResponseStream()";
}
}
catch (Exception ex)
{
result = $"{ex.GetType().Name}: {ex.Message}";
}
return result;
}
The basic pattern there comes from a Xamarin "recipe": Call a REST Web Service. I changed it to be synchronous because when I change request.GetResponse() to request.GetResponseAsync() (and async-await) then the call either times out, or hangs indefinitely, obfuscating the underlying WebException (SecureChannelFailure).
This is a simple HTTP GET, and any browser, or REST/SOAP utility (e.g. "Simple REST Client" for Chrome) can be used to build and send that exact same request, and it works fine with both http and https. Similarly, I can take that code above and paste it into a Windows Console application and it works fine with both http and https.
For the record, love it or hate it, I have also tried adding a delegate of { return true; } to the System.Net.ServicePoint.ServerCertificateAuthenticationCallback event. To me that wouldn't be a solution anyway, but it might help diagnose the problem. Too bad a breakpoint set in that delegate is never tripped, indicating my code never even gets to that point.
It seems impossible to me that invoking web services through https is not supported in a Xamarin Droid app, so I'm going on the assumption that I'm doing something wrong. What could be causing this failure in Xamarin Droid, when it works fine in a browser, and in a plain Windows Console app?
if you call a HTTP url and get this exception , be sure set redirect to false:
request.AllowAutoRedirect = false;
if you call a HTTPS url then put these lines before your request code:
ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;
I have written a program in c# windows application that calls a web service by creating HttpRequest object and gets the response. Everything was working fine as expected in the development phase but now in User Acceptance Testing, whenever the web service is called it is throwing a timeout exception as I am not getting a response after invoking the web service url. But if I invoke the same url from Fiddler, I am getting an immediate response with 200 status code. Below is the code snippet of my program calling a web service url
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(stWebServiceURI);
request.Method = "GET";
request.ContentType = "text/xml";
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
NOTE: A web service call from my application is not getting logged in Fiddler.
Any help on this is much appreciated. Thanks in Advance
I am currently struggling with a very unusual behavior with WCF.
I created a REST endpoint to use with my application, manually tested it out using curl and everything seems to be fine, until I tried to write NUnit tests for it.
The content of my test is basically creating a HttpWebRequest and hit it on my api. Whenever I run the test I get the following error
UnsupportedMediaType Cannot process the message because the content type
'application/json;charset=utf-8;' was not the expected type 'text/xml;
charset=utf-8'.
I have searched for answers, almost all of them is related to mis-configured service, however that cant be the case since a curl request will work.
None explains how come the endpoint only fails with this setup.
Any help would be appreciated
When you send the request you need to specify the type of content. Try this -
HttpWebRequest request = ...
request.ContentType = "text/xml; charset=utf-8";
Also make sure that you are sending XML and not JSON content.
I'm trying to use TweetSharp in a DLL Library.
When I execute the following code in a DLL Library, the response returns invalid (417 Unknown):
var verify = FluentTwitter.CreateRequest()
.AuthenticateWith("<HIDDEN>",
"<HIDDEN>",
"<HIDDEN>",
"<HIDDEN>")
.Statuses().Update("It works!");
var response = verify.Request();
When I execute the same code in a Windows Application, the response returns correctly.
Is there any thing (configuration) that could process HTTP Requests (using POST) differently using the same code in DLL Library and Windows Application?
most likely, the library has the "Expect100Continue" set to true. It should be set to false.
http://groups.google.com/group/twitter-api-announce/browse_thread/thread/7be3b64970874fdd
Looks as though this library needs to be updated. This was changed about 10 months ago.
So the code should something like this...
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(newURL);
request.Method = "POST";
request.ServicePoint.Expect100Continue = false;
request.ContentType = "application/x-www-form-urlencoded";
TweetSharp definitely handles the Expect100Continue issue, so something else is at play here. What does your policy file look like? You need to make sure you can send HTTP requests from DLLs if you're in a medium trust environment by modifying your policy.
Probably its current logged user related
Try to log with your website application pool user and run your code again