【发布时间】:2013-01-08 20:49:23
【问题描述】:
我目前使用下面的 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脚本使用来自linux的wget来执行带有基本认证的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();
}
}
我试过上面的代码出错了:
身份验证字符串:optiweb:optiweb Base64 编码的身份验证字符串:b3B0aXdlYjpvcHRpd2Vi 线程“主”java.io.IOException 中的异常:服务器返回 HTTP 响应代码:500 用于 URL:77.203.65.164:6011 在 sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1403) 在 URLUploader.main(URLUploader.java:32)
有什么问题?
【问题讨论】:
-
你是强调认证一次还是每次认证都可以?
-
我希望它验证一次,然后是多个帖子和回复。