1 package etcom.servlet; 2 3 import java.io.BufferedReader; 4 import java.io.InputStreamReader; 5 import java.io.OutputStream; 6 import java.io.OutputStreamWriter; 7 import java.io.PrintWriter; 8 import java.net.HttpURLConnection; 9 import java.net.Socket; 10 import java.net.URL; 11 import java.util.Scanner; 12 13 /** 14 * 聊天室客户端 15 * @author Administrator 16 * 17 */ 18 public class ClientTest { 19 public static void main(String args[]){ 20 // String urlStr = "http://192.168.1.125:8080/EtcomWeb/ZJJK/TaxCardService"; 21 // String params = "{"mRealReceive":30,"payNo":"17-B0-000014","payType":"宽带费","sUserId":"","clientName":"银行托收16","vcAccNo":"4060459","rowDetail":"<SendData> <Command>build</Command> <Content> <InfoMaster> <ClientName><![CDATA[是的高富帅的]]></ClientName> <ClientTaxCode></ClientTaxCode> <ClientBankAccount><![CDATA[]]></ClientBankAccount> <ClientAddressPhone><![CDATA[]]></ClientAddressPhone> <SellerTaxCode>销方税号</SellerTaxCode > <SellerBankAccount><![CDATA[销方开户银行及帐号]]></SellerBankAccount> <SellerAddressPhone><![CDATA[销方地址及电话]]></SellerAddressPhone> <InfoKind>51</InfoKind> <Notes><![CDATA[发票备注]]></Notes> <Invoicer><![CDATA[]]></Invoicer> <Checker><![CDATA[]]></Checker> <Cashier><![CDATA[]]></Cashier> <IsTaxRate>0</IsTaxRate> </InfoMaster> <InfoDetail> <GoodsName><![CDATA[宽带费]]></GoodsName> <Standard><![CDATA[规格]]></Standard> <Unit><![CDATA[套>></Unit> <Number>1</Number> <Price>30</Price> <Amount>30</Amount> <PriceKind>1</PriceKind> <TaxAmount>1.69811320754717</TaxAmount> <TaxRate>6</TaxRate> <GoodsNoVer>12.0</GoodsNoVer> <GoodsTaxNo>3070599</GoodsTaxNo> <TaxPre>0</TaxPre> <TaxPreCon><![CDATA[]]></TaxPreCon> </InfoDetail> <InfoDetail> </Content> </SendData> "}"; 22 // String urlStr = "http://192.168.1.125:8080/EtcomWeb/androidNew/checkAllowAdd"; 23 // String params = "deptId=12&nqrCode=358"; 24 String urlStr = "http://192.168.1.125:8080/EtcomWeb/android/getFieldsByDeptId"; 25 String params = "deptId=18&typeId=169"; 26 // String urlStr = "http://192.168.1.125:8080/EtcomWeb/androidNew/queryDetectTaskListNew"; 27 // String params = "deptId=12&codeId=358&typeId=330&size=1&vcUserId=&vcHandleFlag=&vcEquipCode="; 28 // String params = "deptId=12&vcUserId=tyc&vcHandleFlag=0&vcStatus=故障"; 29 // String params = "deptId=12&vcUserId=tyc&vcHandleFlag=&vcStatus=&dtSearch=2017-08-17"; 30 // String urlStr = "http://192.168.1.125:8080/EtcomWeb/androidNew/queryDetectTaskDetailList"; 31 // String params = "vcTaskNo=5842e658c8ae024714"; 32 // String urlStr = "http://192.168.1.125:8080/EtcomWeb/androidNew/queryDetectTaskTypeList"; 33 // String params = "deptId=12"; 34 // String urlStr = "http://192.168.1.125:8080/EtcomWeb/androidNew/getFieldsByType"; 35 // String params = "deptId=12&typeId=326&operCode=4857c3c8b55f6946dc3ea73a550ab874"; 36 // String urlStr = "http://192.168.1.125:8080/EtcomWeb/androidNew/execute"; 37 // String params = "detectTaskJOSN={}"; 38 String code = "UTF-8"; 39 String print = getPost(urlStr,params,code); 40 System.out.println(print); 41 } 42 43 /** 44 * 调用外部接口,返回结果串 45 * 使用时注意字符集 "GBK""UTF-8" 46 * 47 * @author ZhangHaiNing 48 * @param urlStr 调用接口的链接 code 接收字符串的编码方式 params 传递参数 49 * @return 50 */ 51 public static String getPost(String urlStr,String params,String code) { 52 try{ 53 URL url = new URL(urlStr); 54 HttpURLConnection con = (HttpURLConnection)url.openConnection(); 55 con.setRequestMethod("POST"); 56 con.setDoOutput(true); // 是否输入参数 57 byte[] bypes = params.toString().getBytes(); 58 //con.setConnectTimeout(5000); //连接超时 单位毫秒 59 //con.setReadTimeout(5000); //读取超时 单位毫秒 60 con.getOutputStream().write(bypes); // 输入参数 61 con.connect(); 62 BufferedReader reader = new BufferedReader(new InputStreamReader(con.getInputStream(),code)); 63 String line; 64 StringBuffer buffer = new StringBuffer(); 65 while((line = reader.readLine()) != null) { 66 buffer.append(line); 67 } 68 reader.close(); 69 con.disconnect(); 70 String res = buffer.toString(); 71 return res; 72 } catch(Exception e) { 73 e.printStackTrace(); 74 } 75 return null; 76 } 77 }