Example usage for org.apache.http.impl.client DefaultHttpClient DefaultHttpClient

List of usage examples for org.apache.http.impl.client DefaultHttpClient DefaultHttpClient

Introduction

In this page you can find the example usage for org.apache.http.impl.client DefaultHttpClient DefaultHttpClient.

Prototype

public DefaultHttpClient() 

Source Link

Usage

From source file:niclients.main.getni.java

/**
 * Main of the GET NI.//from   w  w  w.j  a  va 2  s.com
 */
@SuppressWarnings("unchecked")
public static void main(String[] args) throws UnsupportedEncodingException {
    HttpClient client = new DefaultHttpClient();
    boolean done;
    String dst = null;
    JSONArray loc_array = new JSONArray();
    String c_type;
    HttpResponse response;
    int resp_code = 0;

    if (commandparser(args)) {

        dst = fqdn;
        done = false;

        try {
            while (!done) {
                if (createget(dst, niname)) {

                    response = client.execute(post);
                    resp_code = response.getStatusLine().getStatusCode();

                    if (200 == resp_code) {
                        // Get content type
                        c_type = response.getEntity().getContentType().getValue();

                        if ("application/json".equalsIgnoreCase(c_type)) {
                            // Response is location list
                            InputStream content = response.getEntity().getContent();
                            String resp = convertStreamToString(content);

                            // String to JSONArray
                            Object obj = JSONValue.parse(resp);
                            JSONArray array = (JSONArray) obj;

                            // add new locations to loc_array   
                            for (int i = 0; i < array.size(); i++) {
                                loc_array.add(array.get(i));
                            }
                            // Get next location from the loc_array and remove it from the loc_array
                            if (!loc_array.isEmpty()) {

                                // Check if new dst is type ni://
                                String tmp_dst = loc_array.get(0).toString();
                                tmp_dst = niUtils.mapNiToWKU(loc_array.get(0).toString());

                                if (tmp_dst == null) {
                                    //Check id new dst is type nihttp://
                                    tmp_dst = niUtils.mapNiHttpToWKU(loc_array.get(0).toString());
                                    if (tmp_dst != null)
                                        // is nihttp://
                                        dst = tmp_dst;
                                    else
                                        // is http://
                                        dst = loc_array.get(0).toString();
                                } else {
                                    // is ni://
                                    dst = tmp_dst;
                                }
                                loc_array.remove(0);
                            }
                        } else if ("application/octet-stream".equalsIgnoreCase(c_type)) {
                            // Response is content
                            InputStream content = response.getEntity().getContent();
                            if (output_filename == null)
                                writeCacheCopyToStdOut(content);
                            else {
                                writeCacheCopy(content, output_filename);
                                System.err.println("Content was stored to '" + output_filename + "'");
                            }
                            // so we can end the while
                            done = true;
                        } else {
                            // Response content type is not something we expected
                            System.err.println("Unsupported Content type = " + c_type);
                        }
                    } else {
                        // Response codetype is not success (we expected that)
                        System.err.println("RESP_CODE: " + Integer.toString(resp_code));
                    }
                } else {
                    System.err.println("Command parse failed!");
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

From source file:Main.java

public static DefaultHttpClient getDefaultHttpClient() {
    return new DefaultHttpClient();
}

From source file:Main.java

private static HttpClient getHttpCilent() {
    HttpClient client = new DefaultHttpClient();
    // TODO add authorization keys to header

    return client;
}

From source file:Main.java

public static void get(String url) {

    HttpClient httpclient = new DefaultHttpClient();
    HttpGet httpget = new HttpGet(url);
    try {//from   ww  w .  j  a  v  a2  s .c o m
        httpget.setHeader("Content-Type", "application/json");
        HttpResponse response = httpclient.execute(httpget);
        System.out.println(response.getStatusLine());
    } catch (Exception e) {
        // TODO: handle exception
        System.out.println(e.getMessage());
    }

}

From source file:com.emc.cto.ridagent.rid.test.TestScript.java

public static void main(String args[]) throws SAXException, ParserConfigurationException, URISyntaxException,
        ClientProtocolException, IOException {
    String xmlData = "  <iodef-rid:RID lang=\"en\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:iodef-rid=\"urn:ietf:params:xml:ns:iodef-rid-2.0\" xmlns:iodef=\"urn:ietf:params:xml:ns:iodef-1.42\" xsi:schemaLocation=\"urn:ietf:params:xml:ns:iodef-rid-2.0 iodef-rid-2.0.xsd\">";
    xmlData = xmlData + "<iodef-rid:RIDPolicy MsgType=\"Report\" MsgDestination=\"RIDSystem\">";
    xmlData = xmlData + "<iodef-rid:PolicyRegion region=\"IntraConsortium\"/>";
    xmlData = xmlData + " <iodef:Node>";
    xmlData = xmlData + "   <iodef:NodeName>192.168.1.1</iodef:NodeName>";
    xmlData = xmlData + " </iodef:Node>";
    xmlData = xmlData + "<iodef-rid:TrafficType type=\"Network\"/>";
    xmlData = xmlData + "</iodef-rid:RIDPolicy>";
    xmlData = xmlData + "</iodef-rid:RID>";
    String id = TestScript.httpSend(xmlData, "https://ridtest.emc.com:4590/");
    HttpGet httpget = new HttpGet("http://localhost:1280/federation/RID/" + id + "/report.xml");

    DefaultHttpClient httpclient = new DefaultHttpClient();
    Credentials defaultcreds = new UsernamePasswordCredentials("Administrator", "secret");
    httpclient.getCredentialsProvider().setCredentials(new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT),
            defaultcreds);//from   www .j a v  a 2 s .c  o  m
    httpget.setHeader("User-Agent", "EMC RID System");

    HttpResponse response = httpclient.execute(httpget);
    if (response.getEntity() != null) {

        int code = response.getStatusLine().getStatusCode();
        if (code == 404) {
            System.out.println("Error has occured! Document not found in the xDB");

        } else if (code == 200) {
            System.out.println("Document Successfully saved in the database");

        } else {
            System.out.println("Error could not be determined");
        }
    }
}

From source file:Main.java

public static String getNetPostStirng(String url) {
    HttpClient httpClient = new DefaultHttpClient();
    String data = null;//ww w . ja  va2 s.  co m
    HttpPost httpPost = new HttpPost(url);
    try {
        HttpResponse response = httpClient.execute(httpPost);
        if (response.getStatusLine().getStatusCode() == 200) {
            data = EntityUtils.toString(response.getEntity(), "utf-8");
            //            EntityUtils.toByteArray(response.getEntity());
            //            Log.i("ll", "--->"+data);
        } else {
            return null;
        }
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return data;
}

From source file:Main.java

@SuppressWarnings("deprecation")
public static InputStream openHttpConnection(String urlString) throws IOException {
    DefaultHttpClient httpClient = new DefaultHttpClient();
    HttpGet request = new HttpGet(urlString);
    HttpResponse response = httpClient.execute(request);
    return response.getEntity().getContent();
}

From source file:Main.java

public static String responseContent(String url) throws Exception {
    HttpClient client = new DefaultHttpClient();
    HttpGet request = new HttpGet();
    request.setURI(new URI(url));
    InputStream is = client.execute(request).getEntity().getContent();
    BufferedReader inb = new BufferedReader(new InputStreamReader(is));
    StringBuilder sb = new StringBuilder("");
    String line;/*w  w w.  java  2s  . c  o m*/
    String NL = System.getProperty("line.separator");
    while ((line = inb.readLine()) != null) {
        sb.append(line).append(NL);
    }
    inb.close();
    return sb.toString();
}

From source file:Main.java

public static String responseContentUri(URI uri) throws Exception {
    HttpClient client = new DefaultHttpClient();
    HttpPost request = new HttpPost();
    request.setURI(uri);/*from  w w  w .ja  va 2  s  . co m*/
    InputStream is = client.execute(request).getEntity().getContent();
    BufferedReader inb = new BufferedReader(new InputStreamReader(is));
    StringBuilder sb = new StringBuilder("");
    String line;
    String NL = System.getProperty("line.separator");
    while ((line = inb.readLine()) != null) {
        sb.append(line).append(NL);
    }
    inb.close();
    return sb.toString();
}

From source file:Main.java

public static String HttpGet(String url) {
    HttpClient client = new DefaultHttpClient();
    StringBuilder builder = new StringBuilder();
    HttpGet myget = new HttpGet(url);
    try {/*from   w ww.  jav a2s .  c  om*/
        HttpResponse response = client.execute(myget);
        BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));

        for (String s = reader.readLine(); s != null; s = reader.readLine()) {
            builder.append(s);
        }

        char cr = 65279;
        String t = String.valueOf(cr);
        String resultString = builder.toString();
        resultString = resultString.replace("\t", "").replace(t, "");

        return resultString;

    } catch (Exception e) {
        Log.v("url response", "false");
        e.printStackTrace();
    }
    return null;
}