简体   繁体   English

在邮递员 api 中工作正常,但在 java 中却没有

[英]In postman api works fine but in java it doesn't

I am trying to get a JSON Object from an API while using an API Url.我正在尝试在使用 API Url 时从 API 获取 JSON 对象。 This works perfectly when I test it in Postman, but when I try it in my Spring application, it returns 405 with message(The method is not allowed for the requested URL)当我在 Postman 中测试它时,这完美地工作,但是当我在我的 Spring 应用程序中尝试它时,它返回 405 和消息(该方法不允许用于请求的 URL)

My Java Code:-我的 Java 代码:-

URL tokenURL = new URL("https://something.in/v1/token");
HttpURLConnection tokenConnection = (HttpURLConnection) tokenURL.openConnection();
tokenConnection.setRequestMethod("GET");
tokenConnection.setConnectTimeout(Integer.parseInt(env.getProperty
    ("common.webServiceCall.maxTimeOut")));
tokenConnection.setReadTimeout(Integer.parseInt(env.getProperty
    ("common.webServiceCall.maxTimeOut")));
tokenConnection.setRequestProperty("Content-Type", "application/json");
tokenConnection.setRequestProperty("Accept", "application/json");
tokenConnection.setRequestProperty("X-IBM-Client-Id", "45878d21-469c-b68e-34b1suds34c");
tokenConnection.setRequestProperty("X-IBM-Client-Secret", "ytGThJH4sW7hY2skhJHG65uC7xH7v645fsdfkjgFGHDFgcvhg");
tokenConnection.setDoInput(true);
tokenConnection.setDoOutput(true);  
OutputStream tokenStream = null;
try {
    tokenStream = tokenConnection.getOutputStream();
} catch (RemoteException e) {
    e.printStackTrace();
}
try {
    for(int i = 0; i < 3; i++) {
        tokenConnection.connect();
            if (tokenConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {
                break;
            }
    }
} catch (Exception e) {
    e.printStackTrace();
}
if (tokenConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {
    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader((tokenConnection.getInputStream())));
    StringBuilder serviceResponse = new StringBuilder();
    String serviceResponseLine;
    while ((serviceResponseLine = bufferedReader.readLine()) != null) {
        serviceResponse.append(serviceResponseLine);
    }
    tokenStream.close();
    tokenConnection.disconnect();
    System.out.println(serviceResponse);
} else {
    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader((tokenConnection.getErrorStream())));
    StringBuilder serviceResponse = new StringBuilder();
    String serviceResponseLine;
    while ((serviceResponseLine = bufferedReader.readLine()) != null) {
        serviceResponse.append(serviceResponseLine);
    }
    tokenStream.close();
    tokenConnection.disconnect();
    System.out.println(serviceResponse);
}

I have one suggestion, postman can generate source code of request for different programming languages eg java and JavaScript and command line tool like cURL.我有一个建议,postman 可以为不同的编程语言生成请求的源代码,例如 java 和 JavaScript 以及像 cURL 这样的命令行工具。 I suggest use cURL gives you more verbose details that can help you in connection configuration.我建议使用 cURL 为您提供更详细的详细信息,可以帮助您进行连接配置。

在此处输入图片说明

暂无
暂无

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

相关问题 在PostMan上发布数据有效,而在Java上不起作用 - Post data on PostMan works, whereas on java doesn't work AWS API Gateway - 在Java中使用Spring RestTemplate访问API时的连接超时,使用Postman可以正常工作 - AWS API Gateway - Connection timeout on accessing API using Spring RestTemplate in Java where it works fine using Postman 使用Jersey到API的POST请求出错,适用于POSTMAN - Error on POST request with Jersey to API, works fine with POSTMAN 无法在前端使用 cookie,但在邮递员中工作正常 - Can't use cookie in frontend but works fine in postman React - 第一个文件上传到 Spring API 不起作用,再试一次,第二次尝试正常吗? - React - 1st file upload to Spring API doesn't work, try again and it works fine on second try? 为什么opennlp库的HelloWorld在Java上工作正常,但不适用于Jruby? - Why the HelloWorld of opennlp library works fine on Java but doesn't work with Jruby? Java应用程序在Eclipse上工作正常,但在部署为可运行的jar后它无法正常工作 - a Java application works fine on Eclipse but it doesn't work properly after being deployed as a runnable jar C ++代码可以正常工作,但从Java(JNI)调用时不起作用 - C++ code works fine but doesn't work when calling from Java (JNI) Toast在实际设备上不起作用,但在模拟器上可以正常工作 - Toast doesn't works on real device but works fine on emulator Java程序运行正常但不编译 - Java program runs fine but doesn't compile
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM