httpURLConnection发送http请求


public static String CallJSHttpRequest(String data,String actionType,String invoiceAdderss){
// Properties prop = InfProResourceLoader.loadPropertyFile("");
StringBuffer urlBuf = new StringBuffer();
// String url = prop.getProperty("url");
// String action = prop.getProperty(actionType);

// String url = "http://61.144.36.122:9000/TaxHttpService/tax_";
String url = invoiceAdderss+"/TaxHttpService/tax_";
String action = actionType;
urlBuf.append(url);
urlBuf.append(action);
// if(data!=null && !"".equalsIgnoreCase(data)){
// try {
// urlBuf.append("?inputJson="+URLEncoder.encode(data, "utf-8"));
// } catch (UnsupportedEncodingException e) {
// // TODO 自动生成的 catch 块
// e.printStackTrace();
// }//
//
// }

try {
URL localURL = new URL(urlBuf.toString());
URLConnection connection = localURL.openConnection();
       HttpURLConnection httpURLConnection = (HttpURLConnection)connection;
       httpURLConnection.setDoOutput(true);
       httpURLConnection.setDoInput(true);
       httpURLConnection.setUseCaches(false);
       httpURLConnection.setRequestMethod("POST");
       httpURLConnection.setRequestProperty("Accept-Charset", "utf-8");
       httpURLConnection.setRequestProperty("ContentType", "utf-8");
//        httpURLConnection.setRequestProperty("Content-Length", String.valueOf(data.length()));
       httpURLConnection.setInstanceFollowRedirects(true);
       httpURLConnection.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
       httpURLConnection.setRequestProperty("Connection", "Keep-Alive");
       httpURLConnection.setConnectTimeout(100000);
DataOutputStream out = null;
try {
out = new DataOutputStream(connection.getOutputStream());
}catch (ConnectException e) {
JSONObject json = new JSONObject();
// {"RetMsg":"金税卡已经打开","RetCode":"3001"}
json.put("RetCode", "999");
json.put("RetMsg", "亲,开票服务器未响应,请检查地址是否正确!开票地址:" + invoiceAdderss);
httpURLConnection.disconnect();
// locker = false;
return json.toString();
}catch (Exception e) {
logger.info("服务器无响应 ");
JSONObject json = new JSONObject();
// {"RetMsg":"金税卡已经打开","RetCode":"3001"}
json.put("RetCode", "999");
json.put("RetMsg", "连接失败! " + e.getMessage());
httpURLConnection.disconnect();
// locker = false;
return json.toString();
}
// 发送请求
if (data != null) {
String content = "inputJson="
+ URLEncoder.encode(data, "utf-8");
logger.info("参数发送:" + data);
out.writeBytes(content);
} else {
logger.info("无参数发送.");
}
out.flush();
out.close(); // flush and close


       OutputStream outputStream = null;
       OutputStreamWriter outputStreamWriter = null;
       InputStream inputStream = null;
       InputStreamReader inputStreamReader = null;
       BufferedReader reader = null;
       StringBuffer resultBuffer = new StringBuffer();
       String tempLine = null;
       
       try {
//            outputStream = httpURLConnection.getOutputStream();
//            outputStreamWriter = new OutputStreamWriter(outputStream,"utf-8");
//            
           //数据通过参数传递
//            outputStreamWriter.write(data.toString());
//            outputStreamWriter.flush();
           
           if (httpURLConnection.getResponseCode() >= 300) {
               throw new Exception("HTTP Request is not success, Response code is " + httpURLConnection.getResponseCode());
           }
           
           inputStream = httpURLConnection.getInputStream();
           inputStreamReader = new InputStreamReader(inputStream,"utf-8");
           reader = new BufferedReader(inputStreamReader);
           
           while ((tempLine = reader.readLine()) != null) {
               resultBuffer.append(tempLine);
           }
           
       } catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
} catch (Exception e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
} finally {
           
           if (outputStreamWriter != null) {
               try {
outputStreamWriter.close();
} catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
           }
           
           if (outputStream != null) {
               try {
outputStream.close();
} catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
           }
           
           if (reader != null) {
               try {
reader.close();
} catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
           }
           
           if (inputStreamReader != null) {
               try {
inputStreamReader.close();
} catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
           }
           
           if (inputStream != null) {
               try {
inputStream.close();
} catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
           }
           
       }


       return resultBuffer.toString();
} catch (MalformedURLException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
} catch (ProtocolException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
} catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
return null;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值