httpclient用户名密码认证示例

package com.xs.waybill.eparcel;

import java.io.File;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.UsernamePasswordCredentials;
import org.apache.commons.httpclient.auth.AuthScope;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.StringRequestEntity;
import org.apache.commons.io.FileUtils;

public class Test {
	public static void main(String[] args) throws Exception {
		HttpClient client = new HttpClient();
		
		client.getState().setCredentials(AuthScope.ANY, 
				new UsernamePasswordCredentials("Your username", "Your password"));
		
		PostMethod method = new PostMethod("https://webapi.auspost.com.au/soap/LodgementManagement_MerchantTest_v1");
		method.setRequestHeader("Content-Type", "application/soap+xml;charset=utf-8;action=\"generateLabel\"");
		
		File file = new File(Test.class.getResource("request2.xml").getPath());
		String content = FileUtils.readFileToString(file, "UTF-8");
		StringRequestEntity requestEntity = new StringRequestEntity(content, "text/xml", "UTF-8");
		method.setRequestEntity(requestEntity);
		
		int code = client.executeMethod(method);
		System.out.println(code);
		System.out.println(method.getResponseBodyAsString());
	}
}


你可能感兴趣的:(JavaEE)