Java发送HTTP请求

     前段时间发送http请求,是在javascipt中发送,用的ajax。今天一同事问我有没有可以在java中发送的方法,我看了api后写了这个方法,其中参数可以是xml串,也可以是是参数对,比如名称=值&名称=值.
ExpandedBlockStart.gifContractedBlock.gif    public String send_url(String urlStr, String param) throws Exception dot.gif{
InBlock.gif        StringBuilder tempStr;
ExpandedSubBlockStart.gifContractedSubBlock.gif        
try dot.gif{
InBlock.gif            url 
= new URL(urlStr);
InBlock.gif            url_con 
= (HttpURLConnection) url.openConnection();
InBlock.gif            url_con.setRequestMethod(
"POST");
InBlock.gif            url_con.setDoOutput(
true);
InBlock.gif
InBlock.gif            url_con.getOutputStream().write(param.getBytes());
InBlock.gif            url_con.getOutputStream().flush();
InBlock.gif            url_con.getOutputStream().close();
InBlock.gif            InputStream in 
= url_con.getInputStream();
InBlock.gif            BufferedReader rd 
= new BufferedReader(new InputStreamReader(in));
InBlock.gif            tempStr 
= new StringBuilder();
ExpandedSubBlockStart.gifContractedSubBlock.gif            
while (rd.read() != -1dot.gif{
InBlock.gif                tempStr.append(rd.readLine());
ExpandedSubBlockEnd.gif            }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        }
 finally dot.gif{
InBlock.gif            
if (url_con != null)
InBlock.gif                url_con.disconnect();
ExpandedSubBlockEnd.gif        }

InBlock.gif        
return new String(tempStr);
ExpandedBlockEnd.gif    }
      还可以做个gui界面,做几个JTextField,JTextArea存在这些需要的参数,然后在按钮的ActionListener中处理函数中加入这个发送方法,实现可视化操作。
     偶做了一个不过是给公司专用的,大家在别的地方肯定是用不到这个jar,偶就帖上来。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值