通过身份验证将Java post xml文件发送到url
我目前使用下面的Linux Shell脚本通过身份验证将Java post xml文件发送到url,java,linux,file,shell,Java,Linux,File,Shell,我目前使用下面的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_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,以便:
我尝试了上面的代码,但出现了错误:
验证字符串:optiweb:optiweb
Base64编码的身份验证字符串:b3B0aXdlYjpvcHRpd2Vi
线程“main”java.io.IOException中的异常:服务器返回了URL:77.203.65.164:6011的HTTP响应代码:500
位于sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1403)
位于URLUploader.main(URLUploader.java:32)
有什么问题吗?您可以使用它
您需要在请求中提供正确的http基本身份验证头。在Java中,您可以通过对username:password进行编码并将其与请求一起传递:
URL url = new URL(urlString);
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
conn.setRequestProperty ("Authorization", Base64.encode(username+":"+password));
OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream());
writer.write(data);
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();
其中“数据”包含要发布到服务器的内容 您是强调一次身份验证,还是每次都进行身份验证就可以了?我希望它进行一次身份验证,然后进行多个帖子和响应。它也可以是http摘要身份验证,因此您的答案仅适用于http基本身份验证
wget
支持两种身份验证方法。您好,所以我可以使用这个:writer.write(“/var/www/html/newfile.xml”)?这行得通吗?不,那只是发布字符串“/var/www/html/newfile.xml”。您需要做的是(通过FileInputReader或类似的东西)读入文件并将其发布到字节数组中并使用它?我知道我听起来很懒,但是你能帮我把这部分添加到你的代码样本中吗?
package org.apache.http.examples.client;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
/**
* A simple example that uses HttpClient to execute an HTTP request against
* a target site that requires user authentication.
*/
public class ClientAuthentication {
public static void main(String[] args) throws Exception {
DefaultHttpClient httpclient = new DefaultHttpClient();
try {
httpclient.getCredentialsProvider().setCredentials(
new AuthScope("localhost", 443),
new UsernamePasswordCredentials("username", "password"));
HttpGet httpget = new HttpGet("https://localhost/protected");
System.out.println("executing request" + httpget.getRequestLine());
HttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
System.out.println("----------------------------------------");
System.out.println(response.getStatusLine());
if (entity != null) {
System.out.println("Response content length: " + entity.getContentLength());
}
EntityUtils.consume(entity);
} finally {
// When HttpClient instance is no longer needed,
// shut down the connection manager to ensure
// immediate deallocation of all system resources
httpclient.getConnectionManager().shutdown();
}
}
}
public void testUpload() throws Exception {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(myUploadUrl);
MultipartEntity reqEntity = new MultipartEntity(
HttpMultipartMode.BROWSER_COMPATIBLE);
reqEntity.addPart("string_field",
new StringBody("field value"));
FileBody bin = new FileBody(
new File("/foo/bar/test.png"));
reqEntity.addPart("attachment_field", bin );
httppost.setEntity(reqEntity);
System.out.println("executing request " + httppost.getRequestLine());
HttpResponse response = httpclient.execute(httppost);
HttpEntity resEntity = response.getEntity();
if (resEntity != null) {
String page = EntityUtils.toString(resEntity);
System.out.println("PAGE :" + page);
}
}
URL url = new URL(urlString);
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
conn.setRequestProperty ("Authorization", Base64.encode(username+":"+password));
OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream());
writer.write(data);
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();