您的位置:首页 > 理论基础 > 计算机网络

httpclient用户名密码认证示例

2015-10-12 17:16 267 查看
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());
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: