POST和GET请求,接码
生活随笔
收集整理的這篇文章主要介紹了
POST和GET请求,接码
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
POST和GET請(qǐng)求,接碼
public static void main(String[] args) throws Exception { // //登錄賬號(hào)獲取接口 // String token = sendGet("http://43.240.74.110:9180/service.asmx/UserLoginStr", "name=234150476&psw=cfy13507211042", new HashMap<>()); // System.out.println("登陸成功,您當(dāng)前的token是:"+token); // //利用token獲取項(xiàng)目的手機(jī)號(hào) // String phone = sendGet("http://43.240.74.110:9180/service.asmx/GetHM2Str", "token="+token+"&xmid=3576&sl=1&lx=0&a1=&a2=&pk=&ks=0&rj=234150476", new HashMap<>()); // System.out.println("獲取手機(jī)號(hào)成功,手機(jī)號(hào)為:"+phone); // //利用token獲取獲取驗(yàn)證碼內(nèi)容String patchcode = sendGet("http://43.240.74.110:9180/service.asmx/GetYzm2Str", "token="+token+"&hm="+phone+"&xmid=3576&sf=1", new HashMap<>());System.out.println("獲取驗(yàn)證碼成功,您的驗(yàn)證碼是:"+patchcode); // //銷毀當(dāng)前手機(jī)號(hào) // String killphone = sendGet("http://43.240.74.110:9180/service.asmx/sfHmStr", "token="+token+"&hm="+phone, new HashMap<>()); // System.out.println("銷毀手機(jī)號(hào)狀態(tài):"+killphone);//postint random = (int) Math.random();String sr=sendPost("http://www.jixunjsq.com/user/index/register_handler.html?"+random, "scene=register&usergroup=21&username=cfy1838138153&password=123456");System.out.println(sr);}public static String sendGet(String url, String param, Map<String, String> header) throws UnsupportedEncodingException, IOException {String result = "";BufferedReader in = null;String urlNameString = url + "?" + param;URL realUrl = new URL(urlNameString);// 打開(kāi)和URL之間的連接URLConnection connection = realUrl.openConnection();//設(shè)置超時(shí)時(shí)間connection.setConnectTimeout(5000);connection.setReadTimeout(15000);// 設(shè)置通用的請(qǐng)求屬性if (header!=null) {Iterator<Entry<String, String>> it =header.entrySet().iterator();while(it.hasNext()){Map.Entry<String, String> entry = it.next();System.out.println(entry.getKey()+":"+entry.getValue());connection.setRequestProperty(entry.getKey(), entry.getValue());}}connection.setRequestProperty("accept", "*/*");connection.setRequestProperty("connection", "Keep-Alive");connection.setRequestProperty("user-agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");// 建立實(shí)際的連接connection.connect();// 獲取所有響應(yīng)頭字段Map<String, List<String>> map = connection.getHeaderFields();// 遍歷所有的響應(yīng)頭字段for (String key : map.keySet()) { // System.out.println(key + "--->" + map.get(key));}// 定義 BufferedReader輸入流來(lái)讀取URL的響應(yīng),設(shè)置utf8防止中文亂碼in = new BufferedReader(new InputStreamReader(connection.getInputStream(), "utf-8"));String line;while ((line = in.readLine()) != null) {result += line;}if (in != null) {in.close();}return result;}/*** 向指定 URL 發(fā)送POST方法的請(qǐng)求** @param url* 發(fā)送請(qǐng)求的 URL* @param param* 請(qǐng)求參數(shù),請(qǐng)求參數(shù)應(yīng)該是 name1=value1&name2=value2 的形式。* @return 所代表遠(yuǎn)程資源的響應(yīng)結(jié)果*/public static String sendPost(String url, String param) {PrintWriter out = null;BufferedReader in = null;String result = "";try {URL realUrl = new URL(url);// 打開(kāi)和URL之間的連接URLConnection conn = realUrl.openConnection();// 設(shè)置通用的請(qǐng)求屬性conn.setRequestProperty("accept", "*/*");conn.setRequestProperty("connection", "Keep-Alive");conn.setRequestProperty("user-agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");// 發(fā)送POST請(qǐng)求必須設(shè)置如下兩行conn.setDoOutput(true);conn.setDoInput(true);// 獲取URLConnection對(duì)象對(duì)應(yīng)的輸出流out = new PrintWriter(conn.getOutputStream());// 發(fā)送請(qǐng)求參數(shù)out.print(param);// flush輸出流的緩沖out.flush();// 定義BufferedReader輸入流來(lái)讀取URL的響應(yīng)in = new BufferedReader(new InputStreamReader(conn.getInputStream()));String line;while ((line = in.readLine()) != null) {result += line;}} catch (Exception e) {System.out.println("發(fā)送 POST 請(qǐng)求出現(xiàn)異常!"+e);e.printStackTrace();}//使用finally塊來(lái)關(guān)閉輸出流、輸入流finally{try{if(out!=null){out.close();}if(in!=null){in.close();}}catch(IOException ex){ex.printStackTrace();}}return result;}總結(jié)
以上是生活随笔為你收集整理的POST和GET请求,接码的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 原创:开怀 诗一首
- 下一篇: 这一招可以让pdf整篇自动翻译,pdf翻