【发布时间】: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