我正在开发一个Android应用,我想在其中使用http proxy
选项(比如打开vpn自定义http标头)
我想将socks proxy
与custom http headers
一起用作host
和x-online-host
。
例如在我的大学" youtube.com"被屏蔽并且" google.com"没有被阻止。
所以我希望我的应用程序连接到youtube.com并通过欺骗我连接到的大学服务器获取响应" google.com"
我在互联网上搜索了示例代码,但我找不到......请帮助我。
这是我的代码(不知道为什么不工作):
public String getdata() throws Exception{
BufferedReader in = null;
String data = null;
String proxyadd = "141.0.11.253";
try{
HttpParams httpP = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpP, 2000);
HttpConnectionParams.setSoTimeout(httpP, 2000);
DefaultHttpClient client = new DefaultHttpClient();
URI website = new URI("youtube.com");
HttpGet request = new HttpGet();
request.setParams(httpP);
request.setURI(website);
HttpHost proxy = new HttpHost(proxyadd, 80);
System.setProperty("sun.net.http.allowRestrictedHeaders", "true");
client.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
request.setHeader("Host", "http://google.com");
HttpResponse response = client.execute(request);
int code = response.getStatusLine().getStatusCode();
if(code == 200){
in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
StringBuffer sb = new StringBuffer("");
String l = "";
String nl = System.getProperty("line.seperator");
while((l = in.readLine()) != null){
sb.append(l+nl);
}
in.close();
data = sb.toString();
return data;
}
答案 0 :(得分:0)
proxy for post method:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.json.JSONObject;
import com.youflik.youflik.utils.Util;
import android.util.Log;
public class HttpPostClient {
private static final String TAG = "HttpClient";
public static int statuscode;
public static JSONObject sendHttpPost(String URL, JSONObject jsonObjSend) {
try {
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpPost httpPostRequest = new HttpPost(URL);
StringEntity se;
se = new StringEntity(jsonObjSend.toString());
// Set HTTP parameters
httpPostRequest.setEntity(se);
httpPostRequest.setHeader("Content-type", "application/json");
httpPostRequest.setHeader("Accept-Charset", "utf-8");
long t = System.currentTimeMillis();
HttpResponse response = (HttpResponse) httpclient.execute(httpPostRequest);
Log.i(TAG, "HTTPResponse received in [" + (System.currentTimeMillis()-t) + "ms]");
StatusLine statusline = response.getStatusLine();
statuscode = statusline.getStatusCode();
// Get hold of the response entity (-> the data):
HttpEntity entity = response.getEntity();
if (entity != null) {
// Read the content stream
InputStream instream = entity.getContent();
// convert content stream to a String
try {
String resultString= convertStreamToString(instream);
instream.close();
// Transform the String into a JSONObject
JSONObject jsonObjRecv = new JSONObject(resultString);
return jsonObjRecv;
} catch (Exception e) {
e.printStackTrace();
}
}
}
catch (Exception e)
{
e.printStackTrace();
}
return null;
}
private static String convertStreamToString(InputStream is) {
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
System.out.println("converted string is "+sb.toString());
return sb.toString();
}
}
proxy for get method
public class HttpGetClient {
public static int statuscode;
public static JSONObject sendHttpPost(String URL){
try {
HttpClient httpclient;
httpclient = HttpClientSingalTon.getHttpClienttest();
HttpGet httpGetRequest = new HttpGet(URL);
if(Util.API_TOKEN !=null)
{
System.out.println(Util.API_TOKEN);
}
HttpResponse response;
// System.out.println("executing request " + httpGetRequest.getRequestLine());
// System.out.println("executing request " + httpGetRequest.getMethod());
// System.out.println("executing request " + httpGetRequest.getFirstHeader("X-Auth-Token"));
// System.out.println("executing request Tsting " + httpGetRequest.getHeaders("X-Auth-Token"));
response = httpclient.execute(httpGetRequest);
StatusLine statusline = response.getStatusLine();
statuscode = statusline.getStatusCode();
// Examine the response status
Log.i("Praeda",response.getStatusLine().toString());
// Get hold of the response entity
HttpEntity entity = response.getEntity();
// If the response does not enclose an entity, there is no need
// to worry about connection release
if (entity != null) {
// A Simple JSON Response Read
InputStream instream = entity.getContent();
String result= convertStreamToString(instream);
// now you have the string representation of the HTML request
instream.close();
JSONObject jsonObjRecv = new JSONObject(result);
return jsonObjRecv;
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
private static String convertStreamToString(InputStream is) {
/*
* To convert the InputStream to String we use the BufferedReader.readLine()
* method. We iterate until the BufferedReader return null which means
* there's no more data to read. Each line will appended to a StringBuilder
* and returned as String.
*/
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
System.out.println(sb.toString());
return sb.toString();
}
}