Created
June 8, 2011 06:47
-
-
Save turugina/1013915 to your computer and use it in GitHub Desktop.
Androidエミュレータからproxy越えてHTTP接続
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
package com.example.android.proxytest; | |
import java.io.ByteArrayOutputStream; | |
import java.io.File; | |
import java.io.FileInputStream; | |
import java.io.IOException; | |
import java.util.Properties; | |
import java.util.regex.Matcher; | |
import java.util.regex.Pattern; | |
import org.apache.http.Header; | |
import org.apache.http.HttpHost; | |
import org.apache.http.HttpResponse; | |
import org.apache.http.HttpStatus; | |
import org.apache.http.auth.AuthScope; | |
import org.apache.http.auth.UsernamePasswordCredentials; | |
import org.apache.http.client.methods.HttpGet; | |
import org.apache.http.conn.params.ConnRoutePNames; | |
import org.apache.http.impl.client.DefaultHttpClient; | |
import android.app.Activity; | |
import android.os.AsyncTask; | |
import android.os.Bundle; | |
import android.util.Log; | |
import android.view.View; | |
import android.widget.Button; | |
import android.widget.Toast; | |
public class Main extends Activity { | |
private class Worker extends AsyncTask<Void, Void, String> | |
{ | |
private final Pattern TITLE_PATTERN = | |
Pattern.compile("<title[^>]*>([^<]*)"); | |
@Override | |
protected String doInBackground(Void... arg0) { | |
DefaultHttpClient cli = new DefaultHttpClient(); | |
if ( proxyHost != null ) | |
{ | |
cli.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxyHost); | |
if ( proxyCredentials != null ) | |
{ | |
cli.getCredentialsProvider().setCredentials( | |
new AuthScope(proxyHost.getHostName(), proxyHost.getPort()), | |
proxyCredentials | |
); | |
} | |
} | |
HttpGet req = new HttpGet("http://www.android.com/"); | |
ByteArrayOutputStream os = null; | |
try { | |
HttpResponse res = cli.execute(req); | |
int sc = res.getStatusLine().getStatusCode(); | |
if ( sc != HttpStatus.SC_OK ) | |
{ | |
Log.i("proxytest", "GET failure : " + sc ); | |
return null; | |
} | |
Header h = res.getFirstHeader("Content-Length"); | |
if ( h != null ) | |
{ | |
os = new ByteArrayOutputStream( | |
Integer.parseInt(h.getValue()) | |
); | |
} | |
else | |
{ | |
os = new ByteArrayOutputStream(); | |
} | |
res.getEntity().writeTo(os); | |
String content = os.toString("UTF-8"); | |
Matcher m = TITLE_PATTERN.matcher(content); | |
if ( m.find() ) | |
{ | |
return m.group(1); | |
} | |
} | |
catch (Exception ex) | |
{ | |
Log.e("proxytest", "something wrong with you :-p ", ex); | |
} | |
finally | |
{ | |
if ( os != null ) try { os.close(); } catch (IOException e) { } | |
} | |
return null; | |
} | |
@Override | |
protected void onPostExecute(String result) { | |
Toast.makeText( | |
Main.this, | |
(result != null ? result : "*something wrong*"), | |
Toast.LENGTH_LONG | |
).show(); | |
} | |
} | |
@Override | |
public void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setProxyConfig(); | |
Button button = new Button(this); | |
button.setText("Push Me!"); | |
button.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
new Worker().execute(); | |
} | |
}); | |
setContentView(button); | |
} | |
private HttpHost proxyHost = null; | |
private UsernamePasswordCredentials proxyCredentials = null; | |
private static final String PROPKEY_PROXY_HOST = "http.proxyHost"; | |
private static final String PROPKEY_PROXY_PORT = "http.proxyPort"; | |
private static final String PROPKEY_PROXY_USER = "http.proxyUser"; | |
private static final String PROPKEY_PROXY_PASSWORD = "http.proxyPassword"; | |
private void setProxyConfig() { | |
File propFile = new File("/data/devenv.prop"); | |
if ( propFile.exists() && propFile.canRead() ) | |
{ | |
Properties props = new Properties(); | |
FileInputStream is = null; | |
try | |
{ | |
is = new FileInputStream(propFile); | |
props.load(is); | |
if ( props.containsKey(PROPKEY_PROXY_HOST) ) | |
{ | |
String host = props.getProperty(PROPKEY_PROXY_HOST); | |
int port = Integer.parseInt(props.getProperty(PROPKEY_PROXY_PORT, "8080")); | |
if ( port < 0) { | |
throw new NumberFormatException("proxyPort must be nonnegative"); | |
} | |
proxyHost = new HttpHost(host, port, "http"); | |
} | |
if ( props.containsKey(PROPKEY_PROXY_USER) && props.containsKey(PROPKEY_PROXY_PASSWORD)) | |
{ | |
proxyCredentials = new UsernamePasswordCredentials( | |
props.getProperty(PROPKEY_PROXY_USER), | |
props.getProperty(PROPKEY_PROXY_PASSWORD) | |
); | |
} | |
} | |
catch (IOException ex) | |
{ | |
Log.e("proxytest", "failed to load devenv.prop", ex); | |
} | |
catch (NumberFormatException ex) | |
{ | |
Log.e("proxytest", "illegal http.proxyPort", ex); | |
} | |
finally | |
{ | |
if ( is != null ) | |
try { is.close(); } catch (Exception ex) { } | |
} | |
} | |
if ( proxyHost != null ) | |
{ | |
Log.i("proxytest", "using proxy:" + proxyHost); | |
} | |
else | |
{ | |
Log.i("proxytest", "no proxy available"); | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment