【问题标题】:Java http request to local apache server - UnknownHostException对本地 apache 服务器的 Java http 请求 - UnknownHostException
【发布时间】:2019-05-13 19:42:18
【问题描述】:

我正在 Windows 上运行带有 xampp 的 apache 本地服务器。 我正在尝试使用一些 Http 请求来测试我的 php 后端。

我在尝试连接我的 Java 程序时收到 java.net.UnknownHostException。 我可以交换并下载一个 chrome 扩展来发出请求,但我想稍后使用一个用 java 编写的 android 应用程序访问我的服务器,所以我需要工作代码。

服务器正在运行,我可以用 chrome 访问它。

这是我的java代码(没有导入和打包):

public class Main {

    public static void main(String[] args) {

        RemoteConnecter connector = new RemoteConnecter("", "");

        ArrayList<String> params = new ArrayList<String>();
        params.add("intent=connect");

        System.out.println(connector.request(params));
    }

  }


  public class RemoteConnecter {

    String password;
    String username;

    public RemoteConnecter(String username, String password) {
        this.password = password;
        this.username = username;
    }

    public String request(ArrayList<String> params) {

        String temp = null;

        String urllink = "https://www.Jobads43.wpm";
        URL url = null;
        HttpURLConnection con = null;

        try {
            url = new URL(urllink);
            con = (HttpURLConnection) url.openConnection();
            con.setRequestMethod("POST");
            con.setDoOutput(true);

            //params
            String urlParams = "user=" + username + "&password=" + password;
            for (int i = 0; i < params.size(); i++) {
                urlParams += "&" + params.get(i);
            }

            byte[] postData       = urlParams.getBytes( StandardCharsets.UTF_8 );
            int    postDataLength = postData.length;
            con.setRequestProperty( "Content-Length", Integer.toString( postDataLength ));
            con.setUseCaches( false );
            DataOutputStream output = new DataOutputStream( con.getOutputStream()/*Exception is thrown here*/);
            output.write( postData );

            BufferedReader in = null;
            in = new BufferedReader(new InputStreamReader(con.getInputStream()));

            StringBuffer sb = new StringBuffer();
            String line;


            while((line = in.readLine()) != null) {
                sb.append(line);
            }
            in.close();
            temp = sb.toString();

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

        return temp;
    }
}

这是我的 vhost.conf 的摘录

<VirtualHost *:80>
    ServerAdmin webmaster@dummy-host.example.com
    DocumentRoot "C:\path-is-correct-but-not-shown"
    ServerName Jobads43.wpm
    ServerAlias www.JobAds.local
    <Directory />
          Options FollowSymLinks
          AllowOverride All
          Require all granted
          Order allow,deny
          Allow from all
    </Directory>
    ErrorLog "C:\xampp\htdocs\log"
</VirtualHost>

这是在我的主机中

127.0.0.1           Jobads43.wpm

我很抱歉格式化,我不能做得更好...... 在编辑中它的格式更好

你能帮帮我吗?

更新:
当我使用网址“https://Jobads43.wpm”时,我得到一个 javax.net.ssl.SSLHandshakeException
当我使用网址“http://Jobads43.wpm”时,它的工作原理。
但我需要使用 https 协议来安全地发送我的数据。怎么办?

【问题讨论】:

  • 尝试将urllink 设置为'jobads43.wpm`
  • @KevinO 我已经完成了

标签: java apache http request local


【解决方案1】:

Stel 1:使用 java keytool 创建证书。 Step2:配置apache访问https而不是http。要做到这一点,请点击以下链接,

https://gist.github.com/nguyenanhtu/33aa7ffb6c36fdc110ea8624eeb51e69

第 3 步:在 java 证书文件中导入相同的证书(您在第 1 步中创建),该文件将位于 java 的安全文件夹中。

这将解决握手问题

【讨论】:

  • 文件怎么叫?
  • 请注明您评论的文件名
  • java cert 文件:我有一个 cacerts 文件和一个 blacklist.certs 文件
  • 你要我告诉你如何创建证书或如何在java中导入证书
  • 如何导入证书
猜你喜欢
  • 2017-03-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多