java發送http協議 一般對方都會限定post或者get 這里就是可用的方法
首先是POST方式
傳入參數為json 可改為任何類型
public class HttpPost implements Runnable { private String url_str; public HttpPost () { } public HttpPost (String url, boolean isPost) { this.url_str = url; } @Override public void run() { JSONObject param_json = new JSONObject(); URL url = null; PrintWriter send_out = null; BufferedReader send_in = null; String result = ""; try { //----------------------------------------------------------// //http://blog.csdn.net/thl331860203/article/details/51783434// //JAVA POST請求遠程HTTP接口 // //----------------------------------------------------------// url = new URL(this.url_str); //打開和URL之間的連接 URLConnection connection = url.openConnection(); //設置通用的請求屬性 connection.setRequestProperty("accept", "*/*"); connection.setRequestProperty("connection", "Keep-Alive"); connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); connection.setRequestProperty("user-agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"); end_out = new PrintWriter(connection.getOutputStream());
//發送請求參數 send_out.print(param_json); //flush輸出流的緩沖 send_out.flush();//定義BufferedReader輸入流來讀取URL的響應 send_in = new BufferedReader(new InputStreamReader(connection.getInputStream())); String line; while((line = send_in.readLine()) != null){ result += line; } getResultData(result); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } public void getResultData(String result) { } }
接下來是Get方式
public class HttpGet implements Runnable { private String url_str;public HttpGet() { } public HttpGet(String url, boolean isPost) { this.url_str = url; } @Override public void run() { JSONObject param_json = new JSONObject(); URL url = null; PrintWriter send_out = null; BufferedReader send_in = null; String result = ""; try { //----------------------------------------------------------// //http://blog.csdn.net/thl331860203/article/details/51783434// //JAVA POST請求遠程HTTP接口 // //----------------------------------------------------------// url = new URL(this.url_str); //打開和URL之間的連接 URLConnection connection = url.openConnection(); //設置通用的請求屬性 connection.setRequestProperty("accept", "*/*"); connection.setRequestProperty("connection", "Keep-Alive"); connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); connection.setRequestProperty("user-agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");//建立連接 connection.connect(); //定義BufferedReader輸入流來讀取URL的響應 send_in = new BufferedReader(new InputStreamReader(connection.getInputStream())); String line; while((line = send_in.readLine()) != null){ result += line; } getResultData(result); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } public void getResultData(String result) { } }
關於實現
HttpGet test = new HttpGet(""); Thread thread = new Thread(test); thread.start();
因為JAVA在某版本之后 為了防止協議沒有響應 所以不允許直接使用這個方法 一定要在子線程中使用
以上除了代碼以外都是自己的理解 如果不對 請指出 謝謝