这篇文章主要讲解了java如何模拟发送form-data的请求,内容清晰明了,对此有兴趣的小伙伴可以学习一下,相信大家阅读完之后会有帮助。
创新互联公司是一家朝气蓬勃的网站建设公司。公司专注于为企业提供信息化建设解决方案。从事网站开发,网站制作,网站设计,网站模板,微信公众号开发,软件开发,成都小程序开发,10多年建站对成都集装箱等多个领域,拥有丰富建站经验。我就废话不多说了,大家还是直接看代码吧!
package com.silot.test; import org.apache.http.HttpResponse; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.mime.HttpMultipartMode; import org.apache.http.entity.mime.MultipartEntity; import org.apache.http.entity.mime.content.StringBody; import org.apache.http.impl.client.DefaultHttpClient; import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.nio.charset.Charset; public class TestCli { public static void main(String args[]) throws Exception { MultipartEntity multipartEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE, "------------------------------0ea3fcae38ff", Charset.defaultCharset()); multipartEntity.addPart("skey", new StringBody("哈哈哈哈哈", Charset.forName("UTF-8"))); multipartEntity.addPart("operator", new StringBody("啦啦啦啦", Charset.forName("UTF-8"))); multipartEntity.addPart("VrfKey", new StringBody("渣渣渣", Charset.forName("UTF-8"))); multipartEntity.addPart("StatCode", new StringBody("00", Charset.forName("UTF-8"))); multipartEntity.addPart("mass_id", new StringBody("1234", Charset.forName("UTF-8"))); multipartEntity.addPart("reference_id", new StringBody("21231544", Charset.forName("UTF-8"))); HttpPost request = new HttpPost("http://xiefei.s1.natapp.cc/v1/withdrawCallback"); request.setEntity(multipartEntity); request.addHeader("Content-Type", "Content-Disposition: form-data; boundary=------------------------------0ea3fcae38ff"); DefaultHttpClient httpClient = new DefaultHttpClient(); HttpResponse response = httpClient.execute(request); InputStream is = response.getEntity().getContent(); BufferedReader in = new BufferedReader(new InputStreamReader(is)); StringBuffer buffer = new StringBuffer(); String line = ""; while ((line = in.readLine()) != null) { buffer.append(line); } System.out.println("发送消息收到的返回:" + buffer.toString()); } }
另外有需要云服务器可以了解下创新互联建站www.cdcxhl.com,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。