简体   繁体   English

如何将 HttpGet 更改为 HttpPost?

[英]How can I change HttpGet into HttpPost?

I am new to java and I am learning slowly but surely;我是 Java 新手,我的学习速度很慢但很确定; any insight to this would be greatly appreciated.对此的任何见解将不胜感激。

I have some functional HttpGet code that I want to adapt into HttpPost so that I can open and send the contents of a local JSON file.我有一些功能性的 HttpGet 代码,我想将它们改编成 HttpPost,以便我可以打开和发送本地 JSON 文件的内容。 I've attempted numerous methods but they've all failed and I have now confused myself.我尝试了很多方法,但都失败了,我现在很困惑。

This is the HttpPost code I have converted so far.这是我到目前为止转换的 HttpPost 代码。 It has only the change of HttpGet to HttpPost.它只有 HttpGet 到 HttpPost 的变化。 import org.apache.http.client.methods.HttpPost; is present.存在。 What should I be doing?我该怎么办?

@Component
public class ServiceConnector {
    private final HttpClient client;

    public ServiceConnector() {
        client = HttpClientBuilder.create().build();
    }

    public String post(String url, String acceptHeader, Optional<String> bearerToken) throws UnauthorizedException {
        HttpPost request = new HttpPost(url);
        request.addHeader("Accept", acceptHeader);
        if (bearerToken.isPresent()) {
            request.addHeader("Authorization", "Bearer " + bearerToken.get());
        }

        try {
            HttpResponse response = client.execute(request);

            if (response.getStatusLine().getStatusCode() == 401) {
                throw new UnauthorizedException();
            }
            return EntityUtils.toString(response.getEntity());

        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}

Edited with "post" where "get" existed.使用存在“get”的“post”进行编辑。

You can try something like this, where you prepare the post request with the JSON and then execute it:您可以尝试这样的操作,您可以使用 JSON 准备发布请求,然后执行它:

@Component
public class ServiceConnector {
    private final HttpClient client;


    public ServiceConnector() {
        client = HttpClientBuilder.create().build();
    }
    public String post(String url, String acceptHeader, Optional<String> bearerToken) throws UnauthorizedException {

    try {
        HttpPost request = new HttpPost(url);
        request.addHeader("Accept", acceptHeader);
        if (bearerToken.isPresent()) {
            request.addHeader("Authorization", "Bearer " + bearerToken.get());
        }
        StringEntity params =new StringEntity("details {\"name\":\"myname\",\"age\":\"20\"} ");
    // You could open, read, and convert the file content into a json-string (use GSON lib here)
        request.addHeader("content-type", "application/json");
        request.addHeader("Accept","application/json");
        request.setEntity(params);
        HttpResponse response = client.execute(request);

        // handle response here...
    }catch (Exception ex) {
        // handle exception here
    } finally {
        client.getConnectionManager().shutdown();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Android中发出httppost请求? - How can I make an httppost request in Android? 在Java中,如何仅对协议缓冲区的内容进行HttpPost? - In Java, how can I HttpPost only the contents of a protocol buffer? 如何将令牌添加到HttpPost Json消息 - How can I add a token to a HttpPost Json message 如何使用HttpClient将HttpPost用于HTTPS端点? - How can I do an HttpPost using HttpClient to an HTTPS endpoint? 我在哪里可以测试我的httpPost方法? - Where can I test my httpPost method? Twitter API:有人可以建议如何在HttpPost请求的正文中发送参数吗? - Twitter API : Can someone suggest how can I send parameter in the body of HttpPost request? HttpPost和HttpGet对象都指向url,以便能够与Volley一起使用? - HttpPost and HttpGet object to url so as to be able to use with Volley? HttpPost,HttpGet,HttpPut等的通用通用实现 - Generic all purpose implementation for HttpPost, HttpGet, HttpPut, etc 我如何弄乱我的HttpGet参数编码? - How am I messing up my HttpGet parameter encoding? 如何执行Login Unsing HttpPost方法。 我必须在帖子中发送什么参数,如何获得回复 - How to perform a Login unsing HttpPost method. What Parameters Do I have to send in the Post, and how can I Get the response
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM