Guest User

Untitled

a guest
Apr 22nd, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.85 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.io.OutputStreamWriter;
  5. import java.net.HttpURLConnection;
  6. import java.net.URL;
  7. import java.util.Map;
  8. import java.util.Map.Entry;
  9.  
  10. import com.google.appengine.repackaged.com.google.common.base.StringUtil;
  11.  
  12. public class URLFetch {
  13. private String url;
  14. private String method;
  15. private String cookie;
  16. private String response;
  17. private Integer responseCode;
  18. private boolean isConnected = false;
  19.  
  20. public URLFetch(String url, String method){
  21. this.url = url;
  22. this.method = method;
  23. }
  24.  
  25. public String getCookie() throws IOException{
  26. if(!isConnected){
  27. throw new IOException("まだ接続していません");
  28. }
  29. return this.cookie;
  30. }
  31. public String getResponse() throws IOException{
  32. if(!isConnected){
  33. throw new IOException("まだ接続していません");
  34. }
  35. return this.response;
  36. }
  37. public Integer getResponseCode() throws IOException{
  38. if(!isConnected){
  39. throw new IOException("まだ接続していません");
  40. }
  41. return this.responseCode;
  42. }
  43.  
  44. public void newConnection(String url, String method){
  45. this.url = url;
  46. this.method = method;
  47. this.cookie = null;
  48. this.response = null;
  49. this.responseCode = null;
  50. this.isConnected = false;
  51. }
  52.  
  53. public void doConnect(Map<String, String> params, String cookie) throws IOException{
  54. URL url = new URL(this.url);
  55. HttpURLConnection con = (HttpURLConnection) url.openConnection();
  56. con.setRequestMethod(this.method);
  57. if(method.toLowerCase().equals("get")){
  58. con.setDoInput(true);
  59. if(cookie != null){
  60. con.setInstanceFollowRedirects(false);
  61. con.setRequestProperty("Cookie", cookie);
  62. }
  63. con.connect();
  64. }else if(method.toLowerCase().equals("post")){
  65. if(params != null){
  66. con.setRequestProperty("Content-type", "application/x-www-form-urlencoded");
  67. }
  68. if(cookie != null){
  69. con.setInstanceFollowRedirects(false);
  70. con.setRequestProperty("Cookie", cookie);
  71. }
  72. con.setDoInput(true);
  73. con.setDoOutput(true);
  74.  
  75. con.connect();
  76. if(params != null){
  77. OutputStreamWriter osw = new OutputStreamWriter(con.getOutputStream());
  78. osw.write(makeParams(params));
  79. osw.flush();
  80. osw.close();
  81. }
  82. }
  83. this.cookie = con.getHeaderField("Set-Cookie");
  84. this.responseCode = con.getResponseCode();
  85. BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream(), "UTF-8"));
  86. String result = "";
  87. String line;
  88. while ((line = in.readLine()) != null) {
  89. result += line;
  90. }
  91. this.response = result;
  92. this.isConnected = true;
  93. con.disconnect();
  94. }
  95.  
  96. private String makeParams(Map<String, String> params){
  97. if(params.isEmpty())
  98. return null;
  99. String result = "";
  100. for(Entry<String, String> e: params.entrySet()){
  101. result += e.getKey()+"="+e.getValue()+"&";
  102. }
  103. result = StringUtil.stripSuffix(result, "&");
  104. return result;
  105. }
  106. }
Add Comment
Please, Sign In to add comment