首页 > 解决方案 > 代号一个 UWP 应用程序本地机器服务器 HTTP 问题

问题描述

我无法让我的 Codename One UWP 应用程序通过 HTTP 与在同一台机器上运行的服务器进行通信。该应用程序在连接到 localhost 的模拟器中运行,当它在真正的 Android 设备上运行时,我可以在其 LAN IP 上连接到我的机器。我在这里发现了一个类似的问题:

https://github.com/codenameone/CodenameOne/issues/2056

并添加了 windows.capabilities 构建提示。不幸的是,通信仍然以同样的方式失败。我的 AppxManifest.xml 文件包含以下内容:

<Capabilities>
  <Capability Name="privateNetworkClientServer"/>
  <Capability Name="internetClient"/>
  <uap:Capability Name="picturesLibrary"/>
  <uap:Capability Name="videosLibrary"/>
  <uap:Capability Name="removableStorage"/>
  <uap:Capability Name="musicLibrary"/>
</Capabilities>

堆栈跟踪中的错误与上面的链接相同(消息=发送请求时发生错误。找不到与此错误代码相关的文本。...无法建立与服务器的连接)。服务器永远不会收到请求。

我尝试连接到“127.0.0.1”、“localhost”和我所有的 LAN IP 地址。

我创建了一个显示问题的测试程序。要进行测试,请运行一个程序来捕获发送到端口的流量(Windows 上的http://www.linklogger.com/portpeeker.htm有效)。现在尝试使用您的 LAN IP 连接到它。

import java.io.*;
import com.codename1.io.*;
import com.codename1.ui.*;
import com.codename1.ui.events.ActionEvent;
import com.codename1.ui.layouts.BoxLayout;
import com.codename1.ui.plaf.UIManager;

public class LocalServerTest {
    private final StringBuffer logContents = new StringBuffer();

    private volatile TextArea logTextArea;

    public void init(Object context) {
        Log.install(new Log() {
            @Override
            protected Writer createWriter() throws IOException {
                return new Writer() {
                    @Override
                    public void write(char[] cbuf, int off, int len) throws IOException {
                        logContents.append(new String(cbuf, off, len));
                        if (logTextArea != null) {
                            Display.getInstance().callSerially(() -> {
                                logTextArea.setText(logContents.toString());
                                logTextArea.getParent().revalidate();
                            });
                        }
                    }

                    @Override
                    public void flush() throws IOException {
                    }

                    @Override
                    public void close() throws IOException {
                    }
                };
            }
        });

        UIManager.initFirstTheme("/theme");
    }

    public void start() {
        logTextArea = new TextArea(logContents.toString());
        logTextArea.setRows(40);

        NetworkManager.getInstance().setTimeout(5 * 1000);

        Form hi = new Form("Test local server connection", BoxLayout.y());
        TextField ipAndPortField = new TextField("", "IP:port");
        hi.add(ipAndPortField);
        hi.add(new Button(new Command("Test") {
            @Override
            public void actionPerformed(ActionEvent evt) {
                ConnectionRequest request = new ConnectionRequest() {
                    @Override
                    protected void buildRequestBody(OutputStream os) throws IOException {
                        os.write(new String("Some test data").getBytes("utf-8"));
                    }

                    @Override
                    protected void handleException(Exception err) {
                        Log.p("Could not communicate with server", Log.ERROR);
                        Log.e(err);
                    }
                };
                request.setUrl("http://" + ipAndPortField.getText() + "/test");
                request.setPost(true);
                request.setFailSilently(true);
                request.setTimeout(5 * 1000);
                request.setSilentRetryCount(0);
                request.setCookiesEnabled(false);
                request.setContentType("text/plain");
                request.addResponseCodeListener(e -> {
                    Log.p("Response code received: " + e.getMessage(), Log.INFO);
                });
                request.addResponseListener(e -> {
                    Log.p("Response received: " + e.getMessage(), Log.INFO);
                });
                Log.p("Sending request to " + request.getUrl(), Log.INFO);
                NetworkManager.getInstance().addToQueue(request);
            }
        }));
        hi.add(logTextArea);
        hi.show();
    }

    public void stop() {
    }

    public void destroy() {
    }
}

标签: uwpcodenameone

解决方案


推荐阅读