Java HttpURLConnection POST,conn.getOutputStream()引发异常
我想用HttpURLConnection发一篇帖子。 我尝试了两种方法,但每次都会得到一个例外:Java HttpURLConnection POST,conn.getOutputStream()引发异常,java,http,post,httpurlconnection,Java,Http,Post,Httpurlconnection,我想用HttpURLConnection发一篇帖子。 我尝试了两种方法,但每次都会得到一个例外:conn.getOutputStream() 在这两种情况下,我得到的例外是: java.net.SocketException:操作超时:连接:可能是由于 无效地址 职能1: public void makePost(String title, String comment, File file) { try { URL servlet = new URL("http://"
conn.getOutputStream()代码>
在这两种情况下,我得到的例外是:
java.net.SocketException:操作超时:连接:可能是由于
无效地址
职能1:
public void makePost(String title, String comment, File file) {
try {
URL servlet = new URL("http://" + "www.server.com/daten/web/test/testupload.nsf/upload?CreateDocument");
HttpURLConnection conn=(HttpURLConnection)servlet.openConnection();
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setUseCaches(false);
String boundary = "---------------------------7d226f700d0";
conn.setRequestProperty("Content-type","multipart/form-data; boundary=" + boundary);
//conn.setRequestProperty("Referer", "http://127.0.0.1/index.jsp");
conn.setRequestProperty("Cache-Control", "no-cache");
OutputStream os = conn.getOutputStream(); //exception throws here!
DataOutputStream out = new DataOutputStream(os);
out.writeBytes("--" + boundary + "\r\n");
writeParam(INPUT_TITLE, title, out, boundary);
writeParam(INPUT_COMMENT, comment, out, boundary);
writeFile(INPUT_FILE, file.getName(), out, boundary);
out.flush();
out.close();
InputStream stream = conn.getInputStream();
BufferedInputStream in = new BufferedInputStream(stream);
int i = 0;
while ((i = in.read()) != -1) {
System.out.write(i);
}
in.close();
} catch (Exception e) {
e.printStackTrace();
}
}
或职能2:
public void makePost2(String title, String comment, File file) {
File binaryFile = file;
String boundary = Long.toHexString(System.currentTimeMillis()); // Just generate some unique random value.
URLConnection connection = null;
try {
connection = new URL("http://" + "www.server.com/daten/web/test/testupload.nsf/upload?CreateDocument").openConnection();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
PrintWriter writer = null;
try {
OutputStream output = connection.getOutputStream(); //exception throws here
writer = new PrintWriter(new OutputStreamWriter(output, CHARSET), true); // true = autoFlush, important!
// Send normal param.
writer.println("--" + boundary);
writer.println("Content-Disposition: form-data; name=\""+ INPUT_TITLE +"\"");
writer.println("Content-Type: text/plain; charset=" + CHARSET);
writer.println();
writer.println(title);
// Send binary file.
writer.println("--" + boundary);
writer.println("Content-Disposition: form-data; name=\""+ INPUT_FILE +"\"; filename=\"" + binaryFile.getName() + "\"");
writer.println("Content-Type: " + URLConnection.guessContentTypeFromName(binaryFile.getName()));
writer.println("Content-Transfer-Encoding: binary");
writer.println();
InputStream input = null;
try {
input = new FileInputStream(binaryFile);
byte[] buffer = new byte[1024];
for (int length = 0; (length = input.read(buffer)) > 0;) {
output.write(buffer, 0, length);
}
output.flush(); // Important! Output cannot be closed. Close of writer will close output as well.
} catch (IOException e) {
e.printStackTrace();
} finally {
if (input != null) try { input.close(); } catch (IOException logOrIgnore) {}
}
writer.println(); // Important! Indicates end of binary boundary.
// End of multipart/form-data.
writer.println("--" + boundary + "--");
} catch (IOException e) {
e.printStackTrace();
} finally {
if (writer != null) writer.close();
}
}
根本无法访问URL。URL错误,或者DNS服务器无法解析主机名。尝试使用已知URL进行简单连接,以排除其中一个URL和另一个URL,例如
InputStream response = new URL("http://stackoverflow.com").openStream();
// Consume response.
更新根据评论,您需要使用代理服务器进行HTTP连接。您还需要在Java端配置它。在尝试连接到URL之前添加以下行
System.setProperty("http.proxyHost", "proxy.example.com");
System.setProperty("http.proxyPort", "8080");
只需在运行时执行一次即可
另见:
如果不建立连接(在这种情况下,需要执行一个以上步骤,即连接),则无法进行传输<代码>连接()
应在配置连接后调用(即在连接上设置***()后)
缺少的是:
conn.connect();
java.net.SocketException:Operation timed out:connect:可能是由于地址无效
告诉了一切。我在html文件中有一个表单,它可以向相同的URL发送帖子,并且可以正常工作。什么?你是说,在一个普通的网络浏览器里?如果是,它是否使用HTTP代理服务器?如果是这样,您也应该在Java端配置它。是的!一定是这样!现在试试好了,我更新了答案,添加了更多关于如何在Java端实现这一点的细节。您好@Blitzkr1eg您能添加您的方法“writeParam”吗