欢迎来到天天文库
浏览记录
ID:10942161
大小:18.21 KB
页数:3页
时间:2018-07-09
《java 应用程序需要直接通过 http 协议来访问网络资源》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、packagecom.gpcsoft.srplatform.sms;importjava.net.URLEncoder;importjava.util.Random;importorg.apache.commons.httpclient.HttpClient;importorg.apache.commons.httpclient.NameValuePair;importorg.apache.commons.httpclient.methods.PostMethod;publicclassSendSMSUtil{/***Description:在java程序里面使用Htt
2、pClient来访问http链接*@paramaddress请求地址*@paramaccount请求连接的账号*@parampassword请求连接账号的密码*@parammobile用户移动电话*@paramcontent发送的内容*@paramtype请求方式*@return*@throwsException**@authorliaodyat2012-6-28下午09:13:28*@companyzhzc*/privateStringsendSMSMessage(Stringaddress,Stringaccount,Stringpassword,Stringmob
3、ile,Stringcontent,Stringtype)throwsException{HttpClienthttpclient=newHttpClient();//使用HttpClient访问http链接//以post方式提交PostMethodpostmethod=newPostMethod(address);//给链接添加参数NameValuePair[]postData=newNameValuePair[5];//HttpClient添加参数,并加密参数值postData[0]=newNameValuePair("zh",URLEncoder.encode(a
4、ccount,"UTF-8"));postData[1]=newNameValuePair("mm",URLEncoder.encode(password,"UTF-8"));postData[2]=newNameValuePair("hm",URLEncoder.encode(mobile,"UTF-8"));postData[3]=newNameValuePair("nr",URLEncoder.encode(content,"UTF-8"));postData[4]=newNameValuePair("sms_type",URLEncoder.encode(typ
5、e,"UTF-8"));postmethod.addParameters(postData);//附参数intsendStatus=0;sendStatus=httpclient.executeMethod(postmethod);//请求返回的状态,200表示成功StringresultMessage="";//postmethod.getResponseBodyAsString()求http链接后返回来的值if("200".equals(sendStatus)&&"0".equals(postmethod.getResponseBodyAsString())){re
6、sultMessage="请求成功";}else{resultMessage="请求失败";}returnresultMessage;}publicstaticvoidmain(String[]args)throwsException{SendSMSUtilsc=newSendSMSUtil();Randomrandom=newRandom();inta=random.nextInt(10);intb=random.nextInt(10);intc=random.nextInt(10);intd=random.nextInt(10);inte=random.nextIn
7、t(10);intf=random.nextInt(10);Stringphonecode=a+""+b+""+c+""+d+""+e+""+f;//生成随机码Stringaddress="http://www.baidu.com";//Http访问地址Stringaccount="nbcqjy";Stringpassword="588888";Stringmobile="1383838438";Stringcontent=phonecode+"[Http的Java访问]";Stringtype="74";sc.sendSMSMessag
此文档下载收益归作者所有