URL发送post请求

String content = MessageUtil.createContentMessage(head,data);
URL postUrl = null;
DataOutputStream output = null;
try {
postUrl = new URL("http://183.63.131.106:40011/extService/ghbExtService.do"); //URL请求地址
//postUrl = new URL("http://183.63.131.106:40013/extServiceTest/ghbExtService.do"); //URL请求地址
HttpURLConnection urlcon =  (HttpURLConnection) postUrl.openConnection();
int contentLength = content.getBytes().length;  //获取报文长度
urlcon.setConnectTimeout(1000*15);
urlcon.setReadTimeout(1000*60*2);
urlcon.setRequestMethod("POST");  //post请求方式
urlcon.setUseCaches(false);       //post请求不能使用缓存
urlcon.setRequestProperty("Content-Length", String.valueOf(contentLength));
if("1".equals(content.charAt(5))){
urlcon.setRequestProperty("Content-Type", "application/xml; charset=UTF-8");
}
if("2".equals(content.charAt(5))){
urlcon.setRequestProperty("Content-Type", "www-form-urlencoded");
urlcon.setRequestProperty("charset", "UTF-8");
}
urlcon.setDoInput(true);  //默认为true
urlcon.setDoOutput(true); //默认为true
//urlcon.connect();       //urlcon.getOutputStream()会隐含的进行connect();
output =  new DataOutputStream(urlcon.getOutputStream());
output.writeBytes(content);
output.flush();

output.close();
String str=null;
//if("1".equals(String .valueOf(content.charAt(5)))){
BufferedReader buffer =new BufferedReader(new InputStreamReader(urlcon.getInputStream()));
StringBuffer strBuffer = new StringBuffer();
String str = null;
while((str=buffer.readLine())!=null){
strBuffer.append(str);
}
System.out.println("返回报文为: "+strBuffer);
buffer.close();

//}

urlcon.disconnect();
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值