Integrating an API that uses SSL to connect machines - c#

I want to integrate an API that will allow my application to send requests to a web server. Unfortunately, this API is not well documented, and I have not gotten a reply from the person who supports the web service that released the API. The instructions on how to integrate the API are the following:
All API calls connect to the standard SSL port, and must begin with
https://www.websitename.com/api.php?username=username&password=password&,
followed by the list of parameters expressed as parametername=value&
I am new to C# development. Can you interpret this set of instructions and tell me how I'd go about integrating this API? I mainly am confused about connecting to the SSL port.

This sounds like it's just an HTTP request to that particular URL. You want to use the WebClient class, and possibly call the DownloadString method, depending on what the response contains.

The standard SSL port is 443, but if you're using a library that parses URLs correctly, it will connect to that port automatically if it sees https:// at the beginning. You can use a custom port by providing it after the hostname (separated by a :), such as https://websitename.com:8443/, if you had an SSL service running on port 8443 instead of 443.

Related

WebSockets on .Net / c# without IIS (self hosted)

This has been discussed here but without a conclusion.
I would like to set up a WebSocket server in a self hosted Windows Service where I currently have multiple REST web services running on WCF. My requirements are not that advanced, I just need to consume an incoming stream of data. The problem is that I cannot change the way the data is send, if I could I would just convert it to a streamed HTTP POST, but this is not possible as the protocol is fixed.
According to Microsoft the WebSocket Class should be available from .NET Framework 4.5 and I am using .NET Framework 4.8 that is also supported but nowhere can I find how to self host it. There are many examples using IIS which I do not use.
So what I want to know is, 1) can the WebSocket class be used to self host a WebSocket Server inside a Windows Service?
If so how is it done? Looking for some example code.
EDIT
I think this might be somewhat undocumented but I finally got it to work. The magic seem to be replacing localhost or * with +. The following seem to work hosting the WebSocket while maintaining working WCF web services.
WebSocketsServer.Start("http://+:80/Socket/");
Some of it was described here but I could not make out when to use + or * and what their actual uses are.
When a port is specified, the host element can be replaced with "*" to
indicate that the HttpListener accepts requests sent to the port if
the requested URI does not match any other prefix. For example, to
receive all requests sent to port 8080 when the requested URI is not
handled by any HttpListener, the prefix is http://*:8080/. Similarly,
to specify that the HttpListener accepts all requests sent to a port,
replace the host element with the "+" character. For example,
https://+:8080. The "*" and "+" characters can be present in prefixes
that include paths.
I still do not think this makes sense fully. Anyone have a reference to why this would work?

Determine port in asp.net core

I want to make some microservices with asp.net core (Web Api)
For service discovery I want use Consul where I must register the services via the HTTP API including IP and port.
My question is:
How can I know which port is used within in a service? Is there a way to determine the used port via code?
Yeah, Use HttpContext.Connection.LocalPort to get the local port that the server is listening on.

Handle a HTTPS WebSocket handshake in C#

I have a local c# server running and listening on localhost and a browser connecting to a cloud service running a JavaScript HTML5 implementation of websockets. Standard ws://
This works great over HTTP:// and I can read the header and use it, but I need it to run in HTTPS:// I understand that I have to change to wss:// and this does connect to my server but the header is garbled/encrypted.
I have looked into SslStream but drawn a blank.
How do I handle this WebSocket Secure header in C#?
This question was answered in the comments:
If your app is behind a Proxy load balancer, it would normally manage the SSL for you (accessing your app without encryption).
If you need to manage SSL, complete the SSL handshake first (usually right after you accept the connection) and than parse protocol specific data (HTTP, Websockets, SMTP etc').

HTTP and SignalR

I am recently learning about web sockets in .Net and have just found SignalR which seems like too good to be true in terms of the abstraction of what connection to use and it seems like there are a few signalr clients in different languages which is awesome.
In my current project different resources are being exposed through a RESTful API, and from my understanding of websockets the client needs to upgrade to a web socket connection through a HTTP request/response. Does signalR handle all this handshaking going on?
If there is an initial request/response from a GET request to retrieve a certain resource but they opt to upgrade to a socket connection, does the server give them any sort of response besides the response saying it acknowledges to open up a web socket connection or is the handshake all that occurs before the information is live updated for that particular resource?
Do you think signalR is scalable as opposed to implementing this through a protocol like STOMP where there are a large number of client libraries?
You are making things too complicated. A typical example of using signalr is:
an html file using JavaScript to connect to a signalr Server when the page is loaded. we call this signalr client.
a signalr server written in c#. it can be a winform or console or service.
the signalr Server can call any dll, or webservices or webapi located in the same server, or even in different Server.
then, the client can call any function defined in the signalr server. the server can call any function defined in the client for a particular client or for groups of clients.
also, client x can call client y functions as well.
you can actually forget about Web sockets, signalr choose the most appropriate transport protocol for you. it will choose Web sockets if it is available in both the server and the client.

Intercept HTTP requests

I've some fishy application that makes HTTP requests to a website, i would like to intersect that request and send other data to the server. Is that possible in C#,java or C++?
EDIT: The application isn't mine, i just know the endpoint that it sends http requests
Fiddler might provide the functionality you need. At the very least it may enable you to see what is being sent to the web site.
in Java You can intercept request from Filter
You may want to look into using an HttpModule, whose purpose is to intercept incoming HTTP requests.
The ASP Column: HTTP Modules
Firstly are you aware of how it is connecting to the internet? For example, is it using the settings from Internet Explorer, or is it establishing a direct connection? If the latter, this may be tricky, there is no direct port forwarding as there in Linux, so you'll need some third-party tools to redirect the traffic to a server (which you can write in Java, C++ or C#, I would go for C# if you know it for pure speed of development) In that server you can intercept the request, and then create your own to actually send to the real destination.
Sounds like a cludge, but I think you're stuck with this approach due to the lack of direct port forwarding. You'll have to configure the third-party tool that you use to forward someother well known port to 80, and your server should write to this.

Categories