Unable to connect to the remote server using ClientWebSocket - c#

I'm trying to connect to a web socket server on localhost:9222 using this C# code:
var ws = new ClientWebSocket();
var uri = new Uri("ws://localhost:9222/X/Y/Z", UriKind.Absolute);
await ws.ConnectAsync(uri, CancellationToken.None);
On that last line, I'm getting Unable to connect to the remote server, with inner exception The remote server returned an error: (404) Not Found..
However, I have this JavaScript that connects to the same WebSocket:
var ws = new WebSocket("ws://localhost:9222/X/Y/Z");
And it works great in both IE and Chrome.
I'm not using any proxies.
Is there something I've missed to configure in the C# code?
How can I debug this?
Update:
Using WebSocket4Net instead of the .NET 4.5 ClientWebSocket, I'm getting HTTP 1.1 500: Internal Server Error. However, I'm not sure how I can debug this either.

Related

Cannot create connection to websocket server, however server works in Javascript

I am using System.Net.WebSockets to create a websocket client to a websocket server I am running. The following code is used to create the websocket
Debug.Log("Setting up websocket");
client = new ClientWebSocket();
await client.ConnectAsync(new Uri("ws://192.168.68.93:8001/ws"), System.Threading.CancellationToken.None);
Debug.Log($"Socket status: {client.State}");
The following error is produced:
System.Net.WebSockets.WebSocketException (0x80004005): Unable to connect to the remote server ---> System.Net.Sockets.SocketException (0x80004005): A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.
The confusing part to me, the following code in javascript works perfectly fine
const ws = new WebSocket("ws://192.168.68.93:8001/ws")
And if I log the messages I receive, they are what I expect/work perfectly fine.
What would cause a JS websocket to work, but not a C# one?
My current suspicion is the issue is actually related to the wsserver, I am running it on an esp32, using a library called ESPAsyncWebServer. But I need to know why C# would fail on it when JS works fine

Error when calling asmx endpoint on server with TLS 1.3 only

I have an application that calls an ASMX web service which is situated on another server.
Recently, this server had been reconfigured to disable all TLS versions except for 1.3. This resulted in my application failing to call the ASMX with the following error:
Could not establish secure channel for SSL/TLS with authority '[website]'.
The application was compiled under .NET 4.5 which doesnt have an option to specify to use TLS 1.3. So i recompiled it under 4.8 with the relevant specification to use TLS1.3:
System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls13;
var basicHttpBinding = get_binding(); //returns either basicHTTpsBinding or basicHttpBinding
basicHttpBinding.MaxBufferPoolSize = maxBufferPoolSize;
basicHttpBinding.MaxBufferSize = maxBufferSize;
basicHttpBinding.MaxReceivedMessageSize = maxReceivedMessageSize;
String webServicesEndpointAddress = "url"
var endpointAddress = new EndpointAddress(webServicesEndpointAddress);
new ChannelFactory<customType>(basicHttpBinding, endpointAddress).CreateChannel();
var webServices = new webserviceSoapClient(basicHttpBinding, endpointAddress);
...
//code to call one of the web service functions
This however now results in a new error:
"An error occurred while receiving the HTTP response to [url] This could be due to the service endpoint binding not using the HTTP protocol.
This could also be due to an HTTP request context being aborted by the server (possibly due to the service shutting down). See server logs for more details.
enter code here
Any ideas what could be causing the above error? I am running the application locally in visual studio over windows 10

Credentialed Service Receives 401 Unauthorized from EWS

I have written a windows service in C# to automatically poll an exchange mailbox using EWS.
On my development machine, which is connected to the exchange server network via VPN, it works perfectly.
On the target server the EWS is returning:
Request failed. The remote server returned an error: (401) Unauthorized.(The remote server returned an error: (401) Unauthorized.)
On the same server, I can connect to the EWS URL via a browser using the same credentials as are being provided to the service.
The credentials are provided in the app.config file, and I have triple checked that they are the same on the target server as my development machine.
What could be causing this?
Well as it turned out the issue was that this
_ews.Credentials = new NetworkCredential(Settings.Username, Settings.Password); // Username in the form "domainname\username"
should have been this
_ews.Credentials = new NetworkCredential(Settings.Username, Settings.Password, Settings.Domain); // Domainname and username separate
For some reason the first line worked externally over a VPN but not internally on the domain network itself.

Using C# WebClient with a Proxy - no request made to proxy server?

We have a background operation (Window service) that we want to use through a proxy server.
Basically, we're doing this:
public WebClient GetWebClient(){
var webClient = new WebClient();
webClient.proxy = new WebProxy(Configuration.ProxyHost, Configuration.ProxyPort);
// add a bunch of headers to the WebClient (sessionids, etc.)
return webClient;
}
The proxy is one that we have configured ourselves using FreeProxy.
I've enabled logging and on the machine I'm testing with, and can confirm that requests are being made to the proxy when using it in Firefox.
No authentication is required for the proxy server, except that the IP has to be within our office (which from the Firefox evidence, I assume is not the problem).
However, within our background process, I don't seem to be using the proxy when I use the webclient:
using(var wc = GetWebClient())
using(var s = wc.OpenRead("someurl"))
using(var sr = new StreamReader(s)){
return sr.ReadToEnd();
}
I receive no errors from the proxy however, it seems like we're just going along without it even though the proxy has explicitly been set.
The information seems to return fine, just not through our proxy.
Is there something I'm missing when using a proxy with a WebClient?
edit: more details. If we disable the proxy service on the server, then we get an exception that we can't connect. So it seems like the webclient is attempting to reach out to the proxy, but that traffic is not actually flowing through the proxy.
Inner Exception: SocketException: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond
It turns out that FreeProxy wasn't accepting HTTPS traffic.
I guess the proxy must return the types of traffic it can route, and if it cannot, the webclient does nothing.
Switched to using the Burp suite as our proxy since it can accept HTTPS.
http://portswigger.net/burp/
You either have to configure windows for using the proxy by default, or set the proxy manually in your code, see: http://msdn.microsoft.com/en-us/library/system.net.webclient.proxy(v=VS.100).aspx
You are using the WebClient class correctly as far as I can tell. I am able to see the following request...
using(var client = new WebClient())
{
client.Proxy = new WebProxy("localhost", 8888);
Console.WriteLine(client.DownloadString("http://www.google.com"));
}
in Fiddler running on my local box. Now if I shut Fiddler down, I get the WebException:
Unable to connect to the remote server
With an inner SocketException of:
No connection could be made because the target machine actively refused it 127.0.0.1:8888
So, with that said, my guess is that your proxy is working as intened but it is just not logging the outgoing HTTP request.

Not able to connect https server using SharpSvn in C# , Could not connect to server error

I am developing Windows application in VS2010. I am using SharpSVN to connect with subversion repository. I have tried to get the repository items using svnClient.GetList(targetUri, out list); function. It is working fine for local(file\\:) repository. When I try to connect the https server, it is giving error.
I have used to sample https server given in codespeces. I have tried in tortoiseSVN also to connect. It is saying "could not connect to server". And I have tried through C# and getting the same error.
Please see below for https server url and username and password
https://svn.codespaces.com/cw/com_codespaces_api_net
username: api_guest
password Password1
below code I have used to connect.
using (SvnClient svnClient = new SvnClient())
{
svnClient.Authentication.DefaultCredentials = new System.Net.NetworkCredential("api_guest", "Password1");
SvnTarget targetUri = new SvnUriTarget(new Uri(sourcePath));
var list = new Collection<SvnListEventArgs>();
svnClient.GetList(targetUri, out list);
}
Error Message:
OPTIONS of 'https://svn.codespaces.com/cw/com_codespaces_api_net': could not connect to server (https://svn.codespaces.com)"
I have searched for solution in Google. I am not able to find any help.

Categories