【问题标题】:SignalR Java Client - NegotiationException when starting HubConnectionSignalR Java 客户端 - 启动 HubConnection 时出现 NegotiationException
【发布时间】:2016-01-13 02:24:43
【问题描述】:

我在一个 Android 项目中使用SignalR Java Client 连接到在我的本地主机上运行的服务器(只是一个用 C# 编写的控制台应用程序)。以下是我在 Android 端的代码,在主要活动的 onCreate 方法内。该应用程序在模拟器上运行,因此我将 URL 的 localhost 部分更改为“10.0.2.2”。

try {

    AndroidPlatformComponent com = new AndroidPlatformComponent();
    Platform.loadPlatformComponent(com);

    String url = "http://10.0.2.2:8080/signalr";
    HubConnection connection = new HubConnection(url);
    HubProxy hubProxy = connection.createHubProxy("MyHub");

    connection.start().get();

    hubProxy.on("Send", new SubscriptionHandler2<String, String>() {
        @Override
        public void run(String e1, String e2) {
            Log.d("Server", e1.toString() + " -> " + e2.toString());
        }
    }, String.class, String.class);


}catch (Exception ex){
    ex.printStackTrace();
}

每次我在connection.start().get(); 行收到以下错误:

10-14 20:00:16.135 27165-27165/com.izmyr.projectx W/System.err: java.util.concurrent.ExecutionException: microsoft.aspnet.signalr.client.transport.NegotiationException: There was a problem in the negotiation with the server
10-14 20:00:16.135 27165-27165/com.izmyr.projectx W/System.err:     at microsoft.aspnet.signalr.client.SignalRFuture.get(SignalRFuture.java:112)
10-14 20:00:16.135 27165-27165/com.izmyr.projectx W/System.err:     at microsoft.aspnet.signalr.client.SignalRFuture.get(SignalRFuture.java:102)
10-14 20:00:16.135 27165-27165/com.izmyr.projectx W/System.err:     at com.izmyr.projectx.main.MainActivity$2.onClick(MainActivity.java:85)
10-14 20:00:16.135 27165-27165/com.izmyr.projectx W/System.err:     at android.view.View.performClick(View.java:4084)
10-14 20:00:16.135 27165-27165/com.izmyr.projectx W/System.err:     at android.view.View$PerformClick.run(View.java:16966)
10-14 20:00:16.135 27165-27165/com.izmyr.projectx W/System.err:     at android.os.Handler.handleCallback(Handler.java:615)
10-14 20:00:16.135 27165-27165/com.izmyr.projectx W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:92)
10-14 20:00:16.135 27165-27165/com.izmyr.projectx W/System.err:     at android.os.Looper.loop(Looper.java:137)
10-14 20:00:16.135 27165-27165/com.izmyr.projectx W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:4745)
10-14 20:00:16.135 27165-27165/com.izmyr.projectx W/System.err:     at java.lang.reflect.Method.invokeNative(Native Method)
10-14 20:00:16.135 27165-27165/com.izmyr.projectx W/System.err:     at java.lang.reflect.Method.invoke(Method.java:511)
10-14 20:00:16.135 27165-27165/com.izmyr.projectx W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
10-14 20:00:16.135 27165-27165/com.izmyr.projectx W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-14 20:00:16.135 27165-27165/com.izmyr.projectx W/System.err:     at dalvik.system.NativeStart.main(Native Method)
10-14 20:00:16.135 27165-27165/com.izmyr.projectx W/System.err: Caused by: microsoft.aspnet.signalr.client.transport.NegotiationException: There was a problem in the negotiation with the server
10-14 20:00:16.135 27165-27165/com.izmyr.projectx W/System.err:     at microsoft.aspnet.signalr.client.transport.HttpClientTransport$1.onResponse(HttpClientTransport.java:86)
10-14 20:00:16.135 27165-27165/com.izmyr.projectx W/System.err:     at microsoft.aspnet.signalr.client.http.java.NetworkRunnable.run(NetworkRunnable.java:82)
10-14 20:00:16.135 27165-27165/com.izmyr.projectx W/System.err:     at java.lang.Thread.run(Thread.java:856)
10-14 20:00:16.135 27165-27165/com.izmyr.projectx W/System.err: Caused by: microsoft.aspnet.signalr.client.http.InvalidHttpStatusCodeException: Invalid status code: 400
10-14 20:00:16.135 27165-27165/com.izmyr.projectx W/System.err: Response: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN""http://www.w3.org/TR/html4/strict.dtd">
10-14 20:00:16.135 27165-27165/com.izmyr.projectx W/System.err: <HTML><HEAD><TITLE>Bad Request</TITLE>
10-14 20:00:16.135 27165-27165/com.izmyr.projectx W/System.err: <META HTTP-EQUIV="Content-Type" Content="text/html; charset=us-ascii"></HEAD>
10-14 20:00:16.135 27165-27165/com.izmyr.projectx W/System.err: <BODY><h2>Bad Request - Invalid Hostname</h2>
10-14 20:00:16.135 27165-27165/com.izmyr.projectx W/System.err: <hr><p>HTTP Error 400. The request hostname is invalid.</p>
10-14 20:00:16.135 27165-27165/com.izmyr.projectx W/System.err: </BODY></HTML>
10-14 20:00:16.135 27165-27165/com.izmyr.projectx W/System.err: Headers: [null: HTTP/1.1 400 Bad Request; ]; [Date: Wed, 14 Oct 2015 20:01:57 GMT; ]; [Content-Length: 334; ]; [X-Android-Received-Millis: 1444852816135; ]; [Connection: close; ]; [Content-Type: text/html; charset=us-ascii; ]; [Server: Microsoft-HTTPAPI/2.0; ]; [X-Android-Sent-Millis: 1444852816126; ]; 
10-14 20:00:16.135 27165-27165/com.izmyr.projectx W/System.err:     at microsoft.aspnet.signalr.client.transport.HttpClientTransport.throwOnInvalidStatusCode(HttpClientTransport.java:206)
10-14 20:00:16.135 27165-27165/com.izmyr.projectx W/System.err:     at microsoft.aspnet.signalr.client.transport.HttpClientTransport$1.onResponse(HttpClientTransport.java:76)
10-14 20:00:16.135 27165-27165/com.izmyr.projectx W/System.err:     ... 2 more

我试图从onCreate 获取代码并将其放入AsyncTask,但它也不起作用。

我需要尽快提出任何建议/解决方案。

编辑:以下是我的简单服务器端代码:

using System;
using Microsoft.AspNet.SignalR;
using Microsoft.Owin.Hosting;
using Owin;
using Microsoft.Owin.Cors;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            // This will *ONLY* bind to localhost, if you want to bind to all addresses
            // use http://*:8080 to bind to all addresses. 
            // See http://msdn.microsoft.com/en-us/library/system.net.httplistener.aspx 
            // for more information.
            string url = "http://localhost:8080";
            using (WebApp.Start(url))
            {
                Console.WriteLine("Server running on {0}", url);

                var context = GlobalHost.ConnectionManager.GetHubContext<MyHub>();
                var key = Console.ReadLine();
                while (key != "quit")
                {
                    context.Clients.All.Send("Server", key);
                    key = Console.ReadLine();
                }
            }
        }
    }
    class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            app.UseCors(CorsOptions.AllowAll);
            app.MapSignalR();
        }
    }
    public class MyHub : Hub
    {
        public void Receive(string name, string message)
        {
            Console.WriteLine(name + " " + message);
        }

        public void Send(string name, string message)
        {
            Clients.All.addMessage(name, message);
        }
    }
}

【问题讨论】:

  • 尝试使用 SubscriptionHandler1 而不是 SubscriptionHandler2。此外,请确保您的 Hub 名称与服务器端相同。如果可能,请发布您的服务器应用程序的代码。并在以下stackoverflow.com/questions/32573823/… 阅读我的答案
  • 并尝试使用我上面链接中的“ClientTransport clientTransport ...”
  • 我添加了我的服务器端代码。 SubscriptionHandler 不会有问题,因为我的代码甚至无法到达那一行。它总是在 connection.start().get(); 处抛出异常。使用 ClientTransport 也没有改变这种情况。
  • 您是否检查过从您的模拟器的网络浏览器访问10.0.2.2:8080/signalr 了吗?
  • 我也有类似的错误“java.util.concurrent.ExecutionException: microsoft.aspnet.signalr.client.transport.NegotiationException: There was a problem in the interview with the server”。我在 wifi 上的真实设备上运行应用程序我已经在远程服务器上上传了 signalR 的演示。我正在使用 android studio 运行项目。有人可以指导我吗?

标签: java android signalr signalr-hub signalr.client


【解决方案1】:

来自您的 logcat "... 由:microsoft.aspnet.signalr.client.http.InvalidHttpStatusCodeException: Invalid status code: 400...""...HTTP错误 400。请求主机名无效..."

所以首先检查您的服务器应用程序。在您的 PC/笔记本电脑的网络浏览器中,访问 http://ip:8080/signalr(其中 ip 是您 PC 的 IP 地址)而不是 http://localhost:8080/signalr,然后查看结果。

然后,从您的 android 客户端,将 10.0.2.2 替换为该 IP。

【讨论】:

  • 结果是“Bad Request - Invalid Hostname / HTTP Error 400. The request hostname is invalid.”。可能是什么问题?
  • 如果您使用 IIS Express,请阅读 stackoverflow.com/questions/14725455/…。此外,在 StackOverflow 中搜索许多其他可用主题
【解决方案2】:

我通过将服务器端代码中的string url = "http://localhost:8080"; 行替换为string url = "http://*:8080"; 并以管理员身份运行它解决了这个问题。

【讨论】:

    【解决方案3】:

    要澄清几件事。从接受的答案中,我得到了这个想法。

    • 第 1 步:我已更改位于 username\Documents\IISExpress\config 位置的 applicationhost.config 文件。对于老年人

    • 第 2 步:查找喜欢这个块(根据您的应用程序):

    <site name="SignalRChat" id="11"> <application path="/" applicationPool="Clr4IntegratedAppPool"> <virtualDirectory path="/" physicalPath="D:\Local\Experiment\SignalRAndroid\SignalRAndroid-master\Src\SignalR Server\SignalRChat" /> </application> <bindings> <binding protocol="http" bindingInformation="*:52527:localhost" /> <binding protocol="http" bindingInformation="*:52527:*" /> </bindings> </site> - 第 3 步:在绑定标签中添加以下行。 &lt;binding protocol="http" bindingInformation="*:52527:*" /&gt;

    如下所示

    <site name="SignalRChat" id="11">
                <application path="/" applicationPool="Clr4IntegratedAppPool">
                    <virtualDirectory path="/" physicalPath="D:\Local\Experiment\SignalRAndroid\SignalRAndroid-master\Src\SignalR Server\SignalRChat" />
                </application>
                <bindings>
                    <binding protocol="http" bindingInformation="*:52527:localhost" />
                    <binding protocol="http" bindingInformation="*:52527:*" />
                </bindings>
            </site>
    

    1.保存applicationhost.config文件。

    2. 在管理员模式下运行 Visual Studio。 (必须)

    3.相应更改端口。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-11-18
      • 1970-01-01
      • 2020-04-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-31
      • 2012-03-06
      相关资源
      最近更新 更多