public class TestClient {
public static void main(String args[]){
HttpClient httpClient = new HttpClient();
String uri="http://webservice.webxml.com.cn/WebServices/ChinaZipSearchWebService.asmx";
PostMethod postMethod = new PostMethod(uri);
HostConfiguration hostconfig = httpClient.getHostConfiguration();
hostconfig.setProxy("proxy.test.com.cn", 80);
httpClient.setHostConfiguration(hostconfig);
StringBuilder sb=new StringBuilder();
sb.append("<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:web=\"http://WebXml.com.cn/\">");
sb.append("<soapenv:Header/>");
sb.append("<soapenv:Body>");
sb.append("<web:getSupportCity>");
sb.append("<!--Optional:-->");
sb.append("<web:byProvinceName>山西</web:byProvinceName>");
sb.append("</web:getSupportCity>");
sb.append("</soapenv:Body>");
sb.append("</soapenv:Envelope>");
postMethod.setRequestHeader("SOAPAction", "http://WebXml.com.cn/getSupportCity");
postMethod.setRequestHeader("Content-Type", "text/xml; charset=UTF-8");
StringRequestEntity requestEntity=new StringRequestEntity(sb.toString());
postMethod.setRequestEntity(requestEntity);
int returnCode=0;
try {
returnCode = httpClient.executeMethod(postMethod);
System.out.println(postMethod.getResponseBodyAsString());
} catch (HttpException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("返回状态码:"+returnCode);
}
}
postMethod.setRequestBody("")已经被方法setRequestEntity方法代替了。
头部信息只有Content-Type需要设置,其他不需要设置
SOAPAction头,如果设置必须设置正确的值,不能设置为空;要么不设置。
httpClient获取返回消息是附件的方法:
StringBuilder sb2=new StringBuilder();
sb2.append("<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-
instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"
xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">");
sb2.append("<soap:Body>");
sb2.append("<enValidateImage xmlns=\"http://WebXml.com.cn/\">");
sb2.append("<byString>wanglei</byString>");
sb2.append("</enValidateImage>");
sb2.append("</soap:Body>");
sb2.append("</soap:Envelope>");
postMethod.setRequestHeader("SOAPAction",
"http://WebXml.com.cn/enValidateImage");
postMethod.setRequestHeader("Content-Type", "text/xml; charset=UTF
-8");
StringRequestEntity requestEntity=new StringRequestEntity(sb2.toString());
postMethod.setRequestEntity(requestEntity);
int returnCode=0;
try {
returnCode = httpClient.executeMethod(postMethod);
InputStream in=postMethod.getResponseBodyAsStream();
byte[] ims=new byte[(int)postMethod.getResponseContentLength()];
in.read(ims);
OutputStream out=new FileOutputStream(new File("c:\\longcxm3.gif"));
out.write(ims);
in.close();
out.close();
} catch (HttpException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
postMethod方法获取的返回消息已经没有响应头部了,直接就是附件的二进制流。
使用httpclient3.x和apache的fileuploade控件上传文件的方法(必须在容器里面)
发送请求:
HttpClient httpClient = HttpClientUtil.getInstance();
PostMethod postMethod = null;
File dest = new File(timestampFile, targetFile.getName())
String uri = "http://10.10.10.10:8080/console/centrolServlet"
postMethod = new PostMethod(uri);
Part[] parts = { new FilePart(projectName, dest) };
postMethod.setRequestEntity(new MultipartRequestEntity(parts,postMethod.getParams()));
int status = httpClient.executeMethod(postMethod);
接受请求:
DiskFileItemFactory factory = new DiskFileItemFactory();
factory.setSizeThreshold(4096);
ServletFileUpload upload = new ServletFileUpload(factory);
File tmp = new File(defaultFile, "tmp");
File uploadFile = new File(tmp, uploadPath);
factory.setRepository(tmp);
List<FileItem> items;
items = upload.parseRequest((HttpServletRequest) request);
Iterator<FileItem> files = items.iterator();
while (files.hasNext())
{
FileItem fi = (FileItem) files.next();
String fileName = fi.getName();
if (fileName != null)
{
File fullFile = new File(fi.getName());
saveFile = new File(uploadFile, fullFile.getName());
fi.write(saveFile);
}
}
分享到:
相关推荐
帮助程序员快速从Apache的HttpClient 3.x升级到HttpClient 4.x
2013-11-21 15:31 292,890 httpclient-4.0.2.jar 2017-12-20 12:08 351,132 httpclient-4.1.1.jar 2012-08-03 01:45 451,595 httpclient-4.1.2-sources.jar 2012-08-03 01:44 352,254 httpclient-4.1.2.jar 2012-08-...
import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.HttpException; import org.apache.commons.httpclient.HttpStatus; import org.apache.commons.httpclient.methods....
httpclient4.3.x和其依赖jar包。
httpclient 4.x 与httpclient3.x已经有很大的区别 该资源简单可用 需要下载最新的httpclient4.1.1包及相关的资源包即可使用
Apache_HttpClient4.x简明教程高清PDF版.pdf Apache_HttpClient4.x简明教程高清PDF版.pdf
含tutorial,commons-logging-1.1.1, commons-codec-1.4-bin.zip, httpcomponents-client-4.0.3-bin.zip, httpcomponents-core-4.1-bin.zip
NULL 博文链接:https://chun521521.iteye.com/blog/2422270
JAVA Apache_HttpClient4.x简明教程高清PDF版.pdf
httpclient4.5.jar开发架包,包含所需要依赖的架包 。
HttpClient4.x手动释放底层HTTP连接[借鉴].pdf
《android原生POST、httpClient4.X实现向PHP服务器上传文件》对应源码,博客地址:http://blog.csdn.net/harvic880925/article/details/17565481,奶奶个熊,今天CSDN写个博客都写不成,格式啥啥的完全跟写的时候不...
《httpClient 4.X与PHP服务器通信详解》博文对应源码,博客地址:http://blog.csdn.net/harvic880925/article/details/17416417
基于apache httpclient 4.2.X开发 自动处理redirect url jsoup解析response text log4j 自动生成日志 源代码基于UTF-8编码,如果出现乱码请切换到此编码 压缩包 包含所有jar文件。
HttpClientUtil.java请求工具类
org.apache.commons.httpclient-3.1.jar 用于解决httpclient jar包依赖!!!
4. 调用HttpClient对象的execute(HttpUriRequest request)发送请求,该方法返回一个HttpResponse。 5. 调用HttpResponse的getAllHeaders()、getHeaders(String name)等方法可获取服务器的响应头;调用HttpResponse...
httpclient4.1.2.zip
HttpClient.php类文件,用于模拟get或post请求,源码中基本使用例子
HttpClient3.1 亲测可用,放心使用