这是我的代码:
Gson gson = new Gson();
String path = "C:\\Users\\UserName\\Desktop\\Jsons\\myfile.json";
String url = "http://file:///C:/Users/UserName/Desktop/myPage.html"; // Should I remove http:// ?
String json = readFile(path, StandardCharsets.UTF_8);
HttpClient httpClient = HttpClientBuilder.create().build();
HttpPost post = new HttpPost(url);
StringEntity jsonEntity = new StringEntity(json);
post.setEntity(jsonEntity);
post.setHeader("Content-type", "application/json");
HttpResponse response = httpClient.execute(post);
System.out.println(response);
readFile()
方法在这里(我从另一个stackoverflow回答得到):
public static String readFile(String path, Charset encoding) throws IOException {
byte[] encoded = Files.readAllBytes(Paths.get(path));
return new String(encoded, encoding);
}
我有一个名为" myPage.html"的小html文件。它有一个表单,它接受一个json输入并调用另一个生成另一个json文件的站点。如果我在浏览器中打开我的html文件并粘贴输入json并点击提交,我将获得输出json。所以html工作得很好,但是当我尝试在java中这样做时,它不起作用。我一直收到这个错误:
Exception in thread "main" java.net.UnknownHostException: file
at java.net.Inet6AddressImpl.lookupAllHostAddr(Native Method)
at java.net.InetAddress$2.lookupAllHostAddr(Unknown Source)
at java.net.InetAddress.getAddressesFromNameService(Unknown Source)
at java.net.InetAddress.getAllByName0(Unknown Source)
at java.net.InetAddress.getAllByName(Unknown Source)
at java.net.InetAddress.getAllByName(Unknown Source)
at org.apache.http.impl.conn.SystemDefaultDnsResolver.resolve(SystemDefaultDnsResolver.java:45)
at org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:112)
at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.connect(PoolingHttpClientConnectionManager.java:359)
at org.apache.http.impl.execchain.MainClientExec.establishRoute(MainClientExec.java:381)
at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:237)
at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:185)
at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:89)
at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:111)
at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:185)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:83)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:108)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:56)
at newTest.newDriver.main(newDriver.java:46)
我查了一下stackoverflow,我发现文件字符串应该有三个正斜杠,但是我的文件字符串已经有了,但这并不起作用。
更新#1: 如果我按你说的做,我现在得到这个例外:
Exception in thread "main" org.apache.http.client.ClientProtocolException: URI does not specify a valid host name: C:/Users/Username/Desktop/myPage.html
at org.apache.http.impl.client.CloseableHttpClient.determineTarget(CloseableHttpClient.java:95)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:83)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:108)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:56)
at newTest.newDriver.main(newDriver.java:46)
更新#2:
这是我的html文件myPage.html
。
<form method="POST" action="https://backend.hera.potentiaco.com/simavg">
<textarea name="s" cols="160" rows="40"></textarea><br><br>
<input type="submit">
</form>
我的myfile.json
有一些乱码,比如&#34; sdfg&#34;。如果我将此内容粘贴到上面的html页面并点击提交,我会得到类似的内容:
{
"error": "illegal value"
}
但是我无法在我的java文件中重现同样的事情。
答案 0 :(得分:0)
是的,您的网址应该String url = "C:/Users/UserName/Desktop/myPage.html";
更改并再次发布您的例外