Reading an XML string from a URL in Java

I am retrieving data from a quiet web service in XML format, but unfortunately mine Stringis null. Please help me, where is the problem in my code?

Here is a sample code:

URL url = new URL("http://loxvo.fogbugz.com/api.asp?cmd=logon&email=myemail&password=mypwd");
HttpURLConnection request1 = (HttpURLConnection) url.openConnection();
request1.setRequestMethod("GET");
request1.connect();
InputStream is = request1.getInputStream();
BufferedReader bf_reader = new BufferedReader(new InputStreamReader(is));
String responseData = IOUtils.toString(bf_reader);
System.out.print(responseData);

I tried this url in the rest client: it returns me the correct xml, but mine Stringis here null.

+4
source share
4 answers

You can read the answer, for example

 BufferedReader bufferedReader = 
     new BufferedReader(new InputStreamReader(request1.getInputStream()));
        String line;
        while ((line = bufferedReader.readLine()) != null) {
          System.out.print(line);
        }

Getting the appropriate code. Try

 URL url = new URL("https://loxvo.fogbugz.com/api.asp?cmd=logon&email=myemail&password=mypwd");
+2
source

Try simply passing the InputStream of the request directly to IOUtils.toString (), because IOUtils.toString does the buffering on its own.

request1.getResponseCode(), - Http.

, connect(), getInputStream().

: 302. , . :

try {
   URL url = new URL("http://loxvo.fogbugz.com/api.asp?cmd=logon&email=myemail&   password=mypwd");
   //URL url = new URL("http://www.google.de");
   HttpURLConnection request1 = (HttpURLConnection) url.openConnection();
   request1.setRequestMethod("GET");
   // request1.connect();
   String code = String.valueOf(request1.getResponseCode());
   System.out.println("Error code "+code);
   InputStream is = request1.getInputStream();

   BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(is));
   String line;
   while ((line = bufferedReader.readLine()) != null) {
     System.out.println(line);
   }
   // BufferedReader bf_reader = new BufferedReader(new InputStreamReader(is));
   // String responseData = IOUtils.toString(bf_reader);
   // System.out.print(responseData);
} catch (Exception e) {
    e.printStackTrace();
}
+2

HTTP 302, , .

URL- HTTP HTTPS, . :

OK
Error code 200
<?xml version="1.0" encoding="UTF-8"?><response><error code="1"><![CDATA[Incorrect password or username]]></error></response>

, URL- http. URL- , "".

String reUrl = request1.getHeaderField("Location");
System.out.println("Redirect URL "+reUrl);

.

:

Found
Error code 302
Redirect URL https://loxvo.fogbugz.com/api.asp?cmd=logon&email=myemail&password=mypwd
+2

InputStream String . .

    URL url = new URL("http://loxvo.fogbugz.com/api.asp?cmd=logon&email=myemail&password=mypwd");
    HttpURLConnection request1 = (HttpURLConnection) url.openConnection();
    request1.setRequestMethod("GET");
    request1.connect();
    InputStream is = request1.getInputStream();
    String inputStreamString = new Scanner(is,"UTF-8").useDelimiter("\\A").next();
    System.out.print(inputStreamString);
+1

All Articles