Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/206.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java android应用程序开发中的Http代理选项_Java_Android - Fatal编程技术网

Java android应用程序开发中的Http代理选项

Java android应用程序开发中的Http代理选项,java,android,Java,Android,我正在开发一个android应用程序,我想在其中使用http代理选项,如OpenVPN自定义http头 我想使用带有自定义http头的socks代理作为主机和x-online-host 例如,在我的大学里,youtube.com被屏蔽,google.com没有被屏蔽 因此,我希望我的应用程序连接到youtube.com,并通过愚弄我正在连接到google.com的学院服务器来获取响应 我在互联网上搜索了示例代码,但没有找到…请帮助我 这是我的代码不知道为什么不工作: public String

我正在开发一个android应用程序,我想在其中使用http代理选项,如OpenVPN自定义http头

我想使用带有自定义http头的socks代理作为主机和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;

        }
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();
    }       


}