Created
November 14, 2009 12:50
-
-
Save mix3/234536 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.BufferedReader; | |
import java.io.IOException; | |
import java.io.InputStreamReader; | |
import java.io.OutputStreamWriter; | |
import java.net.HttpURLConnection; | |
import java.net.URL; | |
import java.util.Map; | |
import java.util.Map.Entry; | |
import com.google.appengine.repackaged.com.google.common.base.StringUtil; | |
public class URLFetch { | |
private String url; | |
private String method; | |
private String cookie; | |
private String response; | |
private Integer responseCode; | |
private boolean isConnected = false; | |
public URLFetch(String url, String method){ | |
this.url = url; | |
this.method = method; | |
} | |
public String getCookie() throws IOException{ | |
if(!isConnected){ | |
throw new IOException("まだ接続していません"); | |
} | |
return this.cookie; | |
} | |
public String getResponse() throws IOException{ | |
if(!isConnected){ | |
throw new IOException("まだ接続していません"); | |
} | |
return this.response; | |
} | |
public Integer getResponseCode() throws IOException{ | |
if(!isConnected){ | |
throw new IOException("まだ接続していません"); | |
} | |
return this.responseCode; | |
} | |
public void newConnection(String url, String method){ | |
this.url = url; | |
this.method = method; | |
this.cookie = null; | |
this.response = null; | |
this.responseCode = null; | |
this.isConnected = false; | |
} | |
public void doConnect(Map<String, String> params, String cookie) throws IOException{ | |
URL url = new URL(this.url); | |
HttpURLConnection con = (HttpURLConnection) url.openConnection(); | |
con.setRequestMethod(this.method); | |
if(method.toLowerCase().equals("get")){ | |
con.setDoInput(true); | |
if(cookie != null){ | |
con.setInstanceFollowRedirects(false); | |
con.setRequestProperty("Cookie", cookie); | |
} | |
con.connect(); | |
}else if(method.toLowerCase().equals("post")){ | |
if(params != null){ | |
con.setRequestProperty("Content-type", "application/x-www-form-urlencoded"); | |
} | |
if(cookie != null){ | |
con.setInstanceFollowRedirects(false); | |
con.setRequestProperty("Cookie", cookie); | |
} | |
con.setDoInput(true); | |
con.setDoOutput(true); | |
con.connect(); | |
if(params != null){ | |
OutputStreamWriter osw = new OutputStreamWriter(con.getOutputStream()); | |
osw.write(makeParams(params)); | |
osw.flush(); | |
osw.close(); | |
} | |
} | |
this.cookie = con.getHeaderField("Set-Cookie"); | |
this.responseCode = con.getResponseCode(); | |
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream(), "UTF-8")); | |
String result = ""; | |
String line; | |
while ((line = in.readLine()) != null) { | |
result += line; | |
} | |
this.response = result; | |
this.isConnected = true; | |
con.disconnect(); | |
} | |
private String makeParams(Map<String, String> params){ | |
if(params.isEmpty()) | |
return null; | |
String result = ""; | |
for(Entry<String, String> e: params.entrySet()){ | |
result += e.getKey()+"="+e.getValue()+"&"; | |
} | |
result = StringUtil.stripSuffix(result, "&"); | |
return result; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment