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()); } }
相关文章推荐
- TCP实现P2P通信、TCP穿越NAT的方法、TCP打洞
- httpclient超时设置
- Httppost上传
- 黑马程序员——Java基础--------网络编程
- Linux基础(三)TCP链接数量
- 5G移动网络 V2X
- 判断当前网络是否可用
- deeplearning系列(五)实现一个简单的深度神经网络
- 网络编程 笔记(二) 基于 Windows简单通信
- Android 项目(详解二)—— 网络连接(2)
- 保存一下,回去再看,关于TCP连接的分析的一篇文章
- Android 项目实践(二)——网络连接
- TCP/IP TIME_WAIT状态原理(转)
- HttpURLConnection详解
- 计算机网络(三)HTTP协议相关基础
- iOS开发网络数据之AFNetworking使用
- Cocos发育Visual Studio下一个HttpClient开发环境设置
- 使用nginx当做http的负载均衡
- IOS 9 同时兼容http以及https请求
- HDU 4280 Island Transport 网络流