I'm trying to read metadata from shoutcast stream using ASP.net WebAPI, .net core 2.1. All I need is the headers and not the audio data.
I found out that Shoutcast servers 2+ give stats xml page, but for compatibility reasons, I need to work this out so I can support v1 too. The /7.html does not give the title and genre.
Following is piece of relevant code:
HttpWebRequest request = null;
HttpWebResponse response = null;
request = (HttpWebRequest)WebRequest.Create(server);
request.Headers.Clear();
request.Headers.Add("GET", "/ HTTP/1.0");
// needed to receive metadata information
request.Headers.Add("Icy-MetaData", "1");
request.UserAgent = "WinampMPEG/5.09";
response = (HttpWebResponse)request.GetResponse();
info.Title=response.Headers["icy-name"];
info.Genre=response.Headers["icy-genre"];
When I run this code on IIS Express, or publish and run on IIS, I get this error:
An unhandled exception occurred while processing the request.
HttpRequestException: The server returned an invalid or unrecognized response.
System.Net.Http.HttpConnection.ThrowInvalidHttpResponse()
WebException: The server returned an invalid or unrecognized response.
System.Net.HttpWebRequest.GetResponse()
I have tried WebClient and StreamReader as well but the issue seems to be consistent.
However I tried the same code in Console Application and it seems to work just fine.
How can I get this to work through a WebAPI on IIS?
I had same issue. I used HttpClient and PostAsync to fix the issue.
It is to note that I initially tried my code with dot net framework rather than dot net core and it was working fine, so I think HttpWebResponse is not compatible with dot net core in some cases.
Have a look on below links, if using HttpClient still does not fix your issue. I hope it helps.
https://github.com/dotnet/corefx/issues/14897
https://github.com/dotnet/corefx/issues/30040
Related
I have webclient call in my SSIS package which call an API to get JSON response.
using (var mySSISWebClient = new System.Net.WebClient())
{
mySSISWebClient.Headers[HttpRequestHeader.Accept] = "application/json";
var result = mySSISWebClient.DownloadString(jsonURLwithDate);
}
When I change the URL to use HTTPS, the package is failing at downloadstring call with the following error.
Download failed: The underlying connection was closed: An unexpected error occurred on a send.
It is working fine when I have the URL with HTTP. I am working on it in SSDT 2010. Please help me to resolve this.
Thanks
I think you might need to add the following code to implement TLS1.2:
ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;
based on this article
https://blogs.perficient.com/microsoft/2016/04/tsl-1-2-and-net-support/
I'm working on an app using Visual Studio 2015 Cordova tools on Windows 8.1. Target is also Windows 8.1.
The app is caching HTTP GET request. So the second GET request to the same resource returns a cached response. I have tested after disabling the network adapter and I still get a response with the cached results.
I am using jsforce libray to connect to salesforce.com. I know I can add a timestamp on the url but I would like to find fix not a work around.
Any ideas?
[UPDATE]
Issue is not related to jsforce as it works well on Android. The error is specific to Windows 8.1 and cordova.
As suggested in the question, timestamping the url helps: I used:
var url = "https://api.myurl.com/" + param1 + "?" + new Date().getTime()
I wrote a simple library to add caching to REST requests for Cordova: https://github.com/glauber-md/mobile-simple-web-call#using-this-library .
The library will use a local database (sqlite) to fetch server data and cache it locally where applicable (e.g. HTTP GET requests with cache-related headers).
Once it receives a 304 Response, it will use the cached data.
To send a GET request, you'd use:
wscall.get(
'http://myserver.org/users/1234',
// (Optional) query strings
null,
function(responseData) {
// Do something when the response is successful
},
function(error) {
// Do something when an error happens
}
);
Then the data would come from remote server or local cache depending of the HTTP server response.
Maybe it will help you.
I am not able to get an item through Google Storage Service API:
Google.Apis.Storage.v1.StorageService service;
// ...
Google.Apis.Storage.v1.ObjectsResource.GetRequest request = service.Objects.Get(bucketName, uri);
request.Alt = StorageBaseServiceRequest<GCSObject>.AltEnum.Json;
Google.Apis.Storage.v1.Data resp = request.Execute();
I am getting GoogleApiException
"An Error occurred, but the error response could not be deserialized."
regardless of there specified item exist or not in the storage.
"request.ExecuteAsStream()" returns "Not Found" text only.
The same issue occurs when trying to set ObjectAccessControl through service.ObjectAccessControls.Insert() method.
Do you have any suggestions?
P.S. I am able to upload an item though without any problem.
Check if your URI/Object contains characters that need escaping like '/'.
It might lead (in .NET4) to response not found 404, meaning the object not found on Google cloud.
The google api for .net cannot serialize this error, so you get this useless message ("error response could not be deserialized").
To fix it, add to the web config:
<uri>
<schemeSettings>
<add name="https" genericUriParserOptions="DontUnescapePathDotsAndSlashes" />
</schemeSettings>
</uri>
I explained the scenario here: Slash, Google Cloud Storage & .NET4.
I have been struggling with this for two days. It is so discouraging.
It is a relatively simple api controller:
[HttpPost]
public HttpResponseMessage Save(ReportCreateInputModel model)
{
if (ModelState.IsValid)
{
_service.AddReport(model);
HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, model);
return response;
}
else
{
return Request.CreateResponse(HttpStatusCode.BadRequest);
}
}
Everything works fine when debugging on localhost:xxxx. The model is valid and a new record can be inserted into the database.
But when it is published to the server, I always get a 400: Bad Request. I have tried different browser and am pretty sure it is a server side issue.
Then I tried increase the request size as mentioned in some other posts. But it still cannot work. I could not install remote debugging tool on that server.
Has anyone seen this problem before? Is it something with IIS 6?
I really appreciate any assistance. Thank you!
Update:
It turns out the api POST action is never hit before the Bad Request error is thrown. But why it is fine in local debugging?
Update:
I have added these lines of code in WebApiConfig.cs
var json = config.Formatters.JsonFormatter;
json.SerializerSettings.PreserveReferencesHandling =
Newtonsoft.Json.PreserveReferencesHandling.Objects;
json.SerializerSettings.ReferenceLoopHandling =
Newtonsoft.Json.ReferenceLoopHandling.Ignore;
config.Formatters.Remove(config.Formatters.XmlFormatter);
config.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always;
Now in the jqXHR.responseText I have an exception
"$id":"3","Message":"An error has occurred.",
"ExceptionMessage":"This operation requires IIS integrated pipeline mode.",
"ExceptionType":"System.PlatformNotSupportedException",
"StackTrace":" at System.Web.HttpContext.get_CurrentNotification()
at System.Web.HttpContextWrapper.get_CurrentNotification()
at GetCurrentNotification(Object )
at Newtonsoft.Json.Serialization.DynamicValueProvider.GetValue(Object target)"
Wow! Is it something to do with IIS 6 I am using? Who can tell me what that StackTrace is? Thanks.
Now I find out in here that it is get_CurrentNotification that requires the pipeline thing, which exists only in IIS7. But who can tell us where have I made calls to HttpContext.CurrentNotification?
Try changing the code to this instead to see why you are getting a bad request,
return Request.CreateResponse(HttpStatusCode.BadRequest, ModelState);
You should always include ModelState in your response if you are returning a 400 due to invalid ModelState. That way the clients would have information on what is wrong with their request.
Also, if possible, just for debugging, enable IncludeExceptionDetails always by doing this on your configuration. That way you can get more details in your 400 error response.
config.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always;
Remember that you should IncludeExceptionDetails always only for debugging on production servers. Otherwise, you would be leaking security sensitive information to everyone who can access your service which is bad.
I'm trying to use WebClient.UploadStringAsync method to send some data to server. It works fine when I send POST data, but when using GET, it throws me an error "An exception occurred during a WebClient request."
Here is my code:
WebClient client = new WebClient();
String data = "param1=value1¶m2=value2";
client.UploadStringAsync(new Uri("http://somesite.com"), "GET", data);
Any idea what's going wrong?
Don't use UploadStringAsync for GET. There is DownloadStringAsync designed specifically for that.
Don't use WebClient because it is bound to the UI thread. Use HttpWebRequest instead.
Uploading data for a GET breaks convention.
You might also want to take a look at HTTPClient which you can install via NuGet.