DownloadString works for HTTP but not for HTTPS - https

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/

Related

Socket.io support for WSO2 API Manager Websocket?

Does inbuilt WebSocket API in WSO2 APIM manager supports socket.io/nodejs based endpoint?
I have socket.io based websocket server (in NodeJS) And socket.io javascript client, in between we are using WSO2 API manager for authenticating websocket connections. But WSO2 fails to connect backend server and gives nothing in response.
I used access_token query parameter for passing Bearer token (from JavaScript client) and it seems working as no error. But WSO2 does not forward requests to endpoint.
Same works fine with native WebSocket server-client.
You can add the following logs to <AM_HOME>/repository/conf/log4j.properties to debug the issue further.
For APIM 3.x.x you can change it according to the log4j2 and add to log4j2.properties
log4j.logger.org.wso2.carbon.inbound.endpoint.protocol.websocket.InboundWebsocketSourceHandler=DEBUG log4j.logger.org.wso2.carbon.inbound.endpoint.protocol.websocket.InboundWebsocketResponseSender=DEBUG
log4j.logger.org.wso2.carbon.websocket.transport.WebSocketClientHandler=DEBUG
log4j.logger.org.wso2.carbon.websocket.transport.WebsocketTransportSender=DEBUG
Previous answer was for socket.io JS client, here is the same hack for socket.io python client.
Modify client.py file from /home/user/.local/lib/python3.6/site-packages/engineio
line no. 515
change this,
return ('{scheme}://{netloc}/{path}/?{query}'
'{sep}transport={transport}&EIO=3').format(...
to this,
return ('{scheme}://{netloc}/{path}?{query}'
'{sep}transport={transport}&EIO=3').format(...
Now, instead of directly modifying actual flow we can use conditional parameter.
if self.noslash == 'true':
return ('{scheme}://{netloc}/{path}?{query}'
'{sep}transport={transport}&EIO=3').format(...
else return original statement.
noslash parameter can be fetched from connect() function in both engineio and socketio library of python.
def connect(self, url, headers={}, transports=None,
engineio_path='engine.io', noslash=None):
self.noslash = noslash
Here is the sample connection string for python-socketio
sio.connect('http://localhost:9099', headers={'Authorization':'Bearer 90e8sf10-3s1w-495f-b20d-5a009f63193v'}, transports=['websocket'], socketio_path='/livefeed/v1', noslash='true')
finally I get rid of this issue. Yes, WSO2 APIM websocket supports socket.io js library that will be the first answer to my own query. More on that, here are some findings.
The actual reason for issue was request URL pattern used by socket.io library and WSO2 APIM. Socket.io JS library make the final URL with / at the end of resource (reference), which is not accepted by query separation logic used by WSO2.
So in simple, this is acceptable
ws://localhost:9099/livefeed/v1?EIO=4&transport=websocket
but not this,
ws://localhost:9099/livefeed/v1/?EIO=4&transport=websocket
Solution:
After few discussions with WSO2 team, it was clear that it is impossible to implement changes at their end. So little hack in socket.io.js file worked for me.
line number - 2535
changed this,
_this.opts.path = _this.opts.path.replace(/\/$/, "") + "/";
to this,
if(typeof opts.noslash!== 'undefined' && opts.noslash== "true"){
_this.opts.path = _this.opts.path.replace(/\/$/, "");
}else{
_this.opts.path = _this.opts.path.replace(/\/$/, "") + "/";
}
and created socket using extra argument.
var socket = io.connect("http://localhost:9099?access_token=90e8sf10-3s1w-495f-b20d-5a009f63193v", { transports: ['websocket', 'polling'], path: '/livefeed/v1', noslash: 'true' });
I know this is not a actual solution, but it worked for me.

WebRequest to Shoutcast throws "invalid or unrecognized response" exception

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

Cordova App 8.1 Caching HTTP request

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.

ApiController accept the json in POST on the localhost testing server, but not on the production server. Why?

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.

Using WebClient.UploadStringAsync with GET data

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&param2=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.

Resources