2015-04-29 9 views
5

Mam bardzo prosty kod, który nie działa.HttpURLConnection Wywołanie GET z niepoprawnymi parametrami

Klasa HttoCon:

package test; 

import java.io.BufferedReader; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.net.HttpURLConnection; 
import java.net.URL; 
import java.net.URLConnection; 
import java.net.URLEncoder; 

public class HttpCon { 


public static void main(String[] args) { 
    try { 
     sendGet(); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
} 
    // HTTP GET request 
     private static void sendGet() throws Exception { 

      String url = "http://myweb.com/public/resource/data/stream"; 

      URL obj = new URL(url); 
      HttpURLConnection con = (HttpURLConnection) obj.openConnection(); 

      // optional default is GET 
      con.setRequestMethod("GET"); 


      //add request header 
      con.setRequestProperty("begin", "1430295300000"); 
      con.setRequestProperty("end", "1430297279988"); 
      con.setRequestProperty("id", "140621"); 
      con.setRequestProperty("time", "FIFTEEN_MINUTE"); 

      int responseCode = con.getResponseCode(); 
      System.out.println("\nSending 'GET' request to URL : " + url); 
      System.out.println("Response Code : " + responseCode); 

      BufferedReader in = new BufferedReader(
        new InputStreamReader(con.getInputStream())); 
      String inputLine; 
      StringBuffer response = new StringBuffer(); 

      while ((inputLine = in.readLine()) != null) { 
       response.append(inputLine); 
      } 
      in.close(); 

      //print result 
      System.out.println(response.toString()); 

     } 
} 

Błąd Ślad:

{"fault":{"exceptionType":"MissingServletRequestParameterException","host":"12.205.101.123","cause":"Required Long parameter 'id' is not present","status":"400","query":"/public/resource/data/stream","stackTrace":"org.springframework.web.bind.MissingServletRequestParameterException: Required Long parameter 'id' is not present\n\tat org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter$ServletHandlerMethodInvoker.raiseMissingParameterException(AnnotationMethodHandlerAdapter.java:774)\n\tat org.springframework.web.bind.annotation.support.HandlerMethodInvoker.resolveRequestParam(HandlerMethodInvoker.java:509)\n\tat 

UWAGA:

Gdybym uderzył URL bezpośrednio w przeglądarce działa dobrze.

http://myweb.com/public/resource/data/stream?begin=1430295300000&end=1430297279988&id=140621&time=FIFTEEN_MINUTE

UPDATE:

Korzystanie curl:

curl -X GET http://myweb.com/public/resource/data/stream?begin=1430295300000&end=1430297279988&id=140621&time=FIFTEEN_MINUTE 

Przede curl nie działa, a wyjątkiem jest ten sam -

curl -X GET 'http://myweb.com/public/resource/data/stream?begin=1430295300000&end=1430297279988&id=140621&time=FIFTEEN_MINUTE' 

Ten curl działa poprawnie .

+0

spróbuj con.setRequestProperty ("id", "140621L"); – Pratik

+1

link nie działa – Pratik

+0

@Pratik - zmieniłem link tylko dla celów demo. –

Odpowiedz

5

Tworzysz ciąg zawierający parametry, które następnie zostaną dodane do adresu URL. Utwórz z tego URL URL. Na przykład: -

String this.url = "http://myweb.com/public/resource/data/stream"; 
this.url += "?begin=1430295300000&end=1430297279988&id=140621 
    &time=FIFTEEN_MINUTE"; 
this.urlObj = new URL(this.url); 

Następnie tworzysz połączenie, jak w oryginalnym przykładzie.

1

openConnection ustanowi połączenie i wyda GET. Kolejne ustawienia parametrów zwracanych URLConnection są ignorowane, ponieważ adres URL został już otwarty.

Dodaj parametry do adresu URL jako ciąg kwerendy params tak jak powyższy link i otworzyć tego linku

lub

spróbować odpowiedzieć parametry do serwera (co jest nawet lepsze)

Powiązane problemy