如何使用java来使用Web服务?

时间:2015-02-04 11:05:52

标签: java json http

我有这个错误:

"Conectando: http://graph.facebook.com/pivotalsoftware
Consulta: {"PARAM1": "pivotalsoftware"}
Error ocurrido
java.io.IOException: Server returned HTTP response code: 403 for URL: http://graph.facebook.com/pivotalsoftware "

我无法理解,所以请帮助我!

public class Essai {

public static void main(String[] args) throws MalformedURLException {
    URL url = new URL("http://graph.facebook.com/pivotalsoftware");
    //Insert your JSON query request
    String query = "{'PARAM1': 'pivotalsoftware'}";
    //It change the apostrophe char to double colon char, to form a correct JSON string
    query=query.replace("'", "\"");

    try{
        //make connection
        URLConnection urlc = url.openConnection();
        //It Content Type is so importan to support JSON call
        urlc.setRequestProperty("Content-Type", "application/xml");
        Msj("Conectando: " + url.toString());
        //use post mode
        urlc.setDoOutput(true);
        urlc.setAllowUserInteraction(false);

        //send query
        PrintStream ps = new PrintStream(urlc.getOutputStream());
        ps.print(query);
        Msj("Consulta: " + query);
        ps.close();

        //get result
        BufferedReader br = new BufferedReader(new InputStreamReader(urlc.getInputStream()));
        String l = null;
        while ((l=br.readLine())!=null) {
            Msj(l);
        }
        br.close();
    } catch (Exception e){
        Msj("Error ocurrido");
        Msj(e.toString());
    }
}

private static void Msj(String texto){
    System.out.println(texto);

    // TODO code application logic here
}

2 个答案:

答案 0 :(得分:0)

下面的课程应该会对您有所帮助

    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.PrintStream;
    import java.net.MalformedURLException;
    import java.net.URL;
    import java.net.URLConnection;
    
    public class userAuthetication {
        //5213
        
        
        
        public static void userlogin(String usrname,  String pwd) throws IOException{
            
            try {
                URL url = new URL("http://140.104.135.204:50/Directory-Rest.aspx?Function=Authenticate");
                //Insert your JSON query request
                
                String query = "{'Username':'"+ usrname + "',"+ "'Password':'" + pwd + "'}";
                //It change the apostrophe char to double colon char, to form a correct JSON string
                query=query.replace("'", "\"");
                
                //make connection
                URLConnection urlc = url.openConnection();
                //It Content Type is so importan to support JSON call
                urlc.setRequestProperty("Content-Type", "application/xml");
                Msj("Conectando: " + url.toString());
                //use post mode
                urlc.setDoOutput(true);
                urlc.setAllowUserInteraction(false);
    
                //send query
                PrintStream ps = new PrintStream(urlc.getOutputStream());
                ps.print(query);
                Msj("Consulta: " + query);
                ps.close();
    
                //get result
                BufferedReader br = new BufferedReader(new InputStreamReader(urlc.getInputStream()));
                String l = null;
                while ((l=br.readLine())!=null) {
                    Msj(l);
                }
                
            } catch (MalformedURLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                Msj("Error ocurrido");
                Msj(e.toString());
            }
        }
         private static void Msj(String texto){
           System.out.println(texto);
       }
        
    
    }

答案 1 :(得分:0)

有可能的话,除非确实有必要,否则我建议您不要构建自己的REST客户端(集成URLConnection或其他低级API)。

有几种方法可以做到这一点。我将把您的注意力吸引到一些最受欢迎的产品上:

  • Spring的RestTemplate ,它使用Jackson JSON处理库来处理传入的数据。 (使用Spring WebClient更新);
  • JAX-RS Jersey实现),它具有几种使用不同媒体类型的方式。

这些是创建REST Client的最简单,最受欢迎的执行工具,因此会消耗REST资源。

这是RestTemplate消耗REST资源所需要的一切:

 public static void getRESTResource() {
    final String uri = "your host here;
 
    RestTemplate restTemplate = new RestTemplate();
    String result = restTemplate.getForObject(uri, String.class); //note, that there are many overloaded methods for different variations of parameters;
    CustomType result = restTemplate.getForObject(REST_URI, CustomType.class, params); //to bind incoming data to your CustomType
 }

可以在here中找到JAX-RS客户端的示例。