简体   繁体   English

我需要执行基本身份验证以及将密钥值作为 Http 发布请求的主体发送并获取数据

[英]I need to perform Basic authentication as well as send key value as body for a Http Post request and get data

Basic Authorization for url url 的基本授权

Key value content in body x-www-form-urlencoded正文 x-www-form-urlencoded 中的键值内容

I need to perform both basic auth and pass www-form-urlencoded data in the body.我需要执行基本身份验证并在正文中传递 www-form-urlencoded 数据。 How can i code in java using http to get response data obtained after passing above data to the url.我如何使用 http 在 java 中编码,以获取将上述数据传递给 url 后获得的响应数据。

After I worked on it I got the below code which works.在我处理它之后,我得到了下面的代码。

public void postMethod() throws Exception{
    String result="";
    try{
        Map<String,String> map=new LinkedHashMap<>();
        map.put("key_data","value");
        StringBuilder postdata=new StringBuilder();
        for(Map.Entry<String,String> param:map.entrySet()){
            if(postdata.length()!=0) postdata.append('&');
            postdata.append(URLEncoder.encode(param.getKey(),"UTF-8"));
            postdata.append('=');
            postdata.append(URLEncoder.encode(param.getValue(),"UTF-8"));
        }
        byte[] data=postdata.toString().getBytes("UTF-8");
        String user='admin';
        String pwd='admin';
        String val=user+":"+pwd;
        String url="https://example.com";
        byte[] authEncode=Base64.encodeBase64(val.getBytes());
        String authString=new String(authEncoder);
        URL url1=new URL(url);
        HttpURLConnection urlConnection=(HttpURLConnection)url1.openConnection();
        urlConnection.setRequestMethod("POST");
        urlConnection.setRequestProperty("Authorization","Basic "+authString);
        urlConnection.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
        urlConnection.setRequestProperty("Content-Length",String.valueOf(data.length));
        urlConnection.setDoOutput(true);
        urlConnection.getOutputStream().write(data);
        Reader in=new BufferedReader(new InputStreamReader(urlConnection.getInputStream(),"UTF-8"));
        StringBuilder str=new StringBuilder();
        for(int i;(i=in.read())>=0;)
            str.append((char)i);
        result=str.toString();
    }
    catch(Exception e){
    //handle the exception
    }
    System.out.println(result);
}

I hope this helps and welcome any other method for the above question or optimization of the above code.我希望这对上述问题或上述代码的优化有帮助并欢迎任何其他方法。

postman helps you create code snippet from itself you can use that also: postman 可帮助您从自身创建代码片段,您也可以使用它:

在此处输入图像描述

Now use the code you want:现在使用你想要的代码:

在此处输入图像描述

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

相关问题 使用基本身份验证和请求正文以Java发送HTTP POST请求 - Send HTTP POST request in Java with Basic Auth and request body 通过HTTP发布请求发送键值参数和JSON消息正文 - Send key-value parametres and json message body throught http post request 使用给定的请求正文和标头发送 HTTP 帖子并获得响应 - Send HTTP post with the given Request body and headers and get the respone 基本身份验证请求应该是POST还是GET? - Should a Basic authentication request be POST or GET? 从 Camel 中的 multipart/form-data HTTP POST 请求中获取键值对 - Get key-value pairs from multipart/form-data HTTP POST request in Camel 如何通过HTTP get请求发送授权密钥? - How do I send an authorization key with my HTTP get request? 我在springboot的帖子正文中发送请求它不起作用 - I send request in post body in springboot it is not working 登录 Steam 需要 RSA 密钥。 我已经从 java 11 使用 HttpClient 创建了一个发布请求,但没有获取密钥,但它返回 Body: {“success”:false} - To login to Steam need a RSA key. I have created a post request with HttpClient from java 11 ot get the key but it returns Body : {“success”:false} 使用基本HTTP身份验证发布JSON - POST a JSON with Basic HTTP Authentication 如何发送带有正文的HTTP GET? - How do I send an HTTP GET with a body?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM