POST和GET请求,接码
生活随笔
收集整理的這篇文章主要介紹了
POST和GET请求,接码
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
POST和GET請求,接碼
public static void main(String[] args) throws Exception { // //登錄賬號獲取接口 // String token = sendGet("http://43.240.74.110:9180/service.asmx/UserLoginStr", "name=234150476&psw=cfy13507211042", new HashMap<>()); // System.out.println("登陸成功,您當前的token是:"+token); // //利用token獲取項目的手機號 // 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("獲取手機號成功,手機號為:"+phone); // //利用token獲取獲取驗證碼內容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("獲取驗證碼成功,您的驗證碼是:"+patchcode); // //銷毀當前手機號 // String killphone = sendGet("http://43.240.74.110:9180/service.asmx/sfHmStr", "token="+token+"&hm="+phone, new HashMap<>()); // System.out.println("銷毀手機號狀態:"+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);// 打開和URL之間的連接URLConnection connection = realUrl.openConnection();//設置超時時間connection.setConnectTimeout(5000);connection.setReadTimeout(15000);// 設置通用的請求屬性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)");// 建立實際的連接connection.connect();// 獲取所有響應頭字段Map<String, List<String>> map = connection.getHeaderFields();// 遍歷所有的響應頭字段for (String key : map.keySet()) { // System.out.println(key + "--->" + map.get(key));}// 定義 BufferedReader輸入流來讀取URL的響應,設置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 發送POST方法的請求** @param url* 發送請求的 URL* @param param* 請求參數,請求參數應該是 name1=value1&name2=value2 的形式。* @return 所代表遠程資源的響應結果*/public static String sendPost(String url, String param) {PrintWriter out = null;BufferedReader in = null;String result = "";try {URL realUrl = new URL(url);// 打開和URL之間的連接URLConnection conn = realUrl.openConnection();// 設置通用的請求屬性conn.setRequestProperty("accept", "*/*");conn.setRequestProperty("connection", "Keep-Alive");conn.setRequestProperty("user-agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");// 發送POST請求必須設置如下兩行conn.setDoOutput(true);conn.setDoInput(true);// 獲取URLConnection對象對應的輸出流out = new PrintWriter(conn.getOutputStream());// 發送請求參數out.print(param);// flush輸出流的緩沖out.flush();// 定義BufferedReader輸入流來讀取URL的響應in = new BufferedReader(new InputStreamReader(conn.getInputStream()));String line;while ((line = in.readLine()) != null) {result += line;}} catch (Exception e) {System.out.println("發送 POST 請求出現異常!"+e);e.printStackTrace();}//使用finally塊來關閉輸出流、輸入流finally{try{if(out!=null){out.close();}if(in!=null){in.close();}}catch(IOException ex){ex.printStackTrace();}}return result;}總結
以上是生活随笔為你收集整理的POST和GET请求,接码的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 原创:开怀 诗一首
- 下一篇: 这一招可以让pdf整篇自动翻译,pdf翻