简体   繁体   English

401 来自一台设备的未经授权的错误,但来自另一台设备的成功

[英]401 Unauthorised error from one device but success from another device

I am creating an application in Xamarin Forms for iOS and android.我正在 Xamarin Forms 中为 iOS 和 android 创建一个应用程序。 In VS, the test code works perfectly every time, with a response code of "OK" (200).在VS中,测试代码每次都能完美运行,响应代码为“OK”(200)。 However, in another little project which will work alongside the mobile apps (written in windows forms (.NET)), the EXACT same code, username, password, url, returns error "Unauthorised" (401).但是,在另一个与移动应用程序(以 Windows 窗体 (.NET) 编写)一起工作的小项目中,完全相同的代码、用户名、密码、url 返回错误“未经授权”(401)。

The url in question that doesn't work is http://192.168.8.193/get.cgi?id=26&sid=255 .有问题的网址是http://192.168.8.193/get.cgi?id=26&sid=255 BUT another url of similar function DOES work: http://-Hidden-/get.cgi?id=26&sid=255 .但是另一个类似功能的网址确实有效: http://-Hidden-/get.cgi?id=26&sid=255

public static async Task<string> TestConnection(string url, string username, string password)
        {
            url = url + PanelCommands.GET_URL + PanelCommands.CONNECT_URL;

            if (GetInternetConnectivity())
            { 

                NetworkCredential credential = new NetworkCredential { UserName = username, Password = password };

                HttpClientHandler handler = new HttpClientHandler { Credentials = credential };
                HttpClient client = new HttpClient(handler);
                client.Timeout = new TimeSpan(0, 0, 10);                

                try
                {
                    var response = await client.GetAsync(url).ConfigureAwait(false);

                    return response.StatusCode.ToString();
                }
                catch (Exception e)
                {
                    Debug.WriteLine(e);
                    return "Failed";
                }
            }

I have tried pre-authenticating, adding authentication headers, etc. But nothing seems to work.我尝试过预验证、添加验证标头等。但似乎没有任何效果。

Any help would be appreciated,任何帮助,将不胜感激,

Thanks!谢谢!

The URL in question that doesn't work is http://192.168.8.193/get.cgi?id=26&sid=255 .有问题的网址是http://192.168.8.193/get.cgi?id=26&sid=255

First of all, I think you are missing the port in your URL.首先,我认为您缺少 URL 中的端口。 It should be something like http://192.168.8.193:5000/get.cgi?id=26&sid=255 .它应该类似于http://192.168.8.193:5000/get.cgi?id=26&sid=255

Edit: This is something I got wrong.编辑:这是我弄错的地方。 You can perfectly have a URL without a specific port.您可以完美地拥有一个没有特定端口的 URL。

Secondly, your firewall may be blocking the connection between your projects, if they are running in different devices.其次,如果您的项目在不同的设备上运行,您的防火墙可能会阻止它们之间的连接。 You can try adding an inbound rule following this article for Windows 10 (it is somewhat similar in other versions of windows as well).您可以尝试按照本文为 Windows 10 添加入站规则(在其他版本的 Windows 中也有些类似)。

Sacked this off.解雇了这个。 Rewrote the project in Xamarin UWP.在 Xamarin UWP 中重写了项目。 Worked perfectly first time.第一次完美地工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从一台设备到另一台设备的表面上的StretchRectangle - StretchRectangle on surface from one device to another 将音频从一台设备传输到另一台设备 - Stream audio from one device to another 收到401未经授权的错误 - 401 Unauthorised Error Received 将文件从一台服务器复制到另一台设备时,设备尚未准备就绪 - The device is not ready when copying a file from one server to another 从另一台设备访问Azure仿真器 - Accessing Azure Emulator from Another Device 使用 Pomelo.EntityFrameworkCore.MySql v6 从 .network 上的另一台设备连接时出错 - Error while connecting from another device on the network,using Pomelo.EntityFrameworkCore.MySql v6 我已经从GCM服务器{“成功”:1}得到了此响应,但是通知没有到达设备 - I've Got This Response From The GCM Server {“success”:1} But the Notification not Come to the Device C#/Windows:从扬声器播放一个音频文件,从“默认音频设备”(耳机/耳机)播放另一个音频文件? - C#/Windows: Play one audio file from speaker, and another audio file from the 'default audio device' (headphones/headset)? Oauth,Twitter,401未经授权 - Oauth, Twitter, 401 Unauthorised 使用 directShow 从 USB 设备中选择另一个来源 - Choose another source from usb device using directShow
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM