我目前使用下面的 Linux Shell 脚本
shell_exec("wget --output-document=/var/www/html/kannel/rresults/".$file.".res --post-file=/var/www/html/kannel/rbilling/".$file." --http-user=user1 --http-password=pass1 --header=\"Content-Type: text/xml\" 77.203.65.164:6011");
此 shell 脚本使用wget
来自 linux 的基本身份验证执行 url 并上传文件。
我想将其转换为 java 以便它将:
- 连接
- 认证
- 发布 XML 文件
另外,是否可以进行一次身份验证,然后发布我想要的尽可能多的文件?
更新.....................我尝试了下面的代码
public class URLUploader {
public static void main(String[] args) throws IOException
{
URL url = new URL("http://77.203.65.164:6011");
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
String name = "user";
String password = "password";
String authString = name + ":" + password;
System.out.println("auth string: " + authString);
byte[] authEncBytes = Base64.encodeBase64(authString.getBytes());
String authStringEnc = new String(authEncBytes);
System.out.println("Base64 encoded auth string: " + authStringEnc);
conn.setRequestProperty("Authorization", "Basic " + authStringEnc);
OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream());
writer.write("/var/www/html/kannel/javacode/13569595024298.xml");
writer.flush();
String line;
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
writer.close();
reader.close();
}
}
i tried the code above go the error:
auth string: optiweb:optiweb
Base64 encoded auth string: b3B0aXdlYjpvcHRpd2Vi
Exception in thread "main" java.io.IOException: Server returned HTTP response code: 500 for URL: 77.203.65.164:6011
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1403)
at URLUploader.main(URLUploader.java:32)
有什么问题?