如何添加参数以及如何获取字符串? (java,httpclient 4.X)

发布于 2024-11-04 03:35:18 字数 785 浏览 0 评论 0原文

代码 Http get 请求:

HttpClient httpclient = new DefaultHttpClient();

    CookieStore cookieStore = new BasicCookieStore();

    // Create local HTTP context
    HttpContext localContext = new BasicHttpContext();
    // Bind custom cookie store to the local context
    localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);

    HttpGet first = new HttpGet("http://vk.com");
    HttpResponse response = httpclient.execute(first, localContext);

    HttpEntity entity = response.getEntity();
    if (entity != null) {
        InputStream instream = entity.getContent();
        int l;
        byte[] tmp = new byte[2048];
        while ((l = instream.read(tmp)) != -1) {
        }
    }

如何获取响应 STRING ?

我需要创建请求 POST、添加参数和自动重定向。

Code Http get request:

HttpClient httpclient = new DefaultHttpClient();

    CookieStore cookieStore = new BasicCookieStore();

    // Create local HTTP context
    HttpContext localContext = new BasicHttpContext();
    // Bind custom cookie store to the local context
    localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);

    HttpGet first = new HttpGet("http://vk.com");
    HttpResponse response = httpclient.execute(first, localContext);

    HttpEntity entity = response.getEntity();
    if (entity != null) {
        InputStream instream = entity.getContent();
        int l;
        byte[] tmp = new byte[2048];
        while ((l = instream.read(tmp)) != -1) {
        }
    }

How get responce STRING ?

And I need create request POST, add params and auto redirect.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

伊面 2024-11-11 03:35:18

可能有一种更快的方法来执行此操作,但这将使您得到字符串形式的响应:

        InputStream = response.getEntity().getContent();
        StringBuilder sb = new StringBuilder();
        BufferedReader r = new BufferedReader(new InputStreamReader(in));
        for (String line = r.readLine(); line != null; line = r.readLine()) {
            sb.append(line);
        }
        in.close();
        String s = sb.toString();

要创建发布请求,请使用:

        PostMethod post = new PostMethod("http://url.com");
        post.addParameter("param1name", "param1value");

或 HttpPost: http://www.androidsnippets.com/executing-a-http-post-request-with-httpclient

HttpClient 应该为您处理自动重定向

There might be a quicker way to do this but this will get you the response as a string:

        InputStream = response.getEntity().getContent();
        StringBuilder sb = new StringBuilder();
        BufferedReader r = new BufferedReader(new InputStreamReader(in));
        for (String line = r.readLine(); line != null; line = r.readLine()) {
            sb.append(line);
        }
        in.close();
        String s = sb.toString();

To create a post request use:

        PostMethod post = new PostMethod("http://url.com");
        post.addParameter("param1name", "param1value");

Or HttpPost: http://www.androidsnippets.com/executing-a-http-post-request-with-httpclient

HttpClient should handle the automatic redirects for you

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文