简体   繁体   English

从Java中的URL读取XML字符串

[英]Read XML String from URL in Java

I am retrieving data from restful web service in XML format, but unfortunately my String is null . 我正在从XML格式的restful Web服务中检索数据,但不幸的是我的Stringnull Please help me, where is the problem in my code? 请帮帮我,我的代码中的问题在哪里?

Here is the example 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 have tried this url in rest client: it's returning me correct xml, but here my String is null . 我在rest客户端尝试了这个url:它返回正确的xml,但是我的Stringnull

You can read response like 你可以阅读响应

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

Getting resp 302 your code. 获取代码。 Try with 试试吧

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

Try to just pass the InputStream of the request directly to IOUtils.toString( ), because IOUtils.toString makes buffering on its own. 尝试将请求的InputStream直接传递给IOUtils.toString(),因为IOUtils.toString自己进行缓冲。

Additionally you can check with request1.getResponseCode() if the request was successfull or if there was any kind of Http Error. 另外,如果请求成功或者存在任何类型的Http错误,您可以使用request1.getResponseCode()进行检查。

Furthermore it is not necessary to invoke the connect() method because this is implicitely done within the getInputStream() call. 此外,没有必要调用connect()方法,因为这在getInputStream()调用中是隐含的。

Update: Getting also Error 302. Of course i d'ont have the correct login data. 更新:获取错误302.当然我有正确的登录数据。 But the request must work using the following code: 但请求必须使用以下代码:

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();
}

The problem is the 302 HTTP Error which means that the server performs a redirect. 问题是302 HTTP错误,这意味着服务器执行重定向。

If you change your URL from HTTP to HTTPS than it works in the expected way. 如果您将URL从HTTP更改为HTTPS,而不是按预期方式工作。 The result of the request is: 请求的结果是:

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

You can get the redirect address by evaluating the response header of the request with the http URL. 您可以通过使用http URL评估请求的响应头来获取重定向地址。 There you can find the redirect URL by checking the value of the key "Location". 在那里,您可以通过检查“位置”键的值来找到重定向URL。

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

.

Which leads to to following output: 这导致以下输出:

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

Try to convert InputStream to String with Scanner. 尝试使用Scanner将InputStream转换为String。 I tried this code and works fine. 我试过这段代码,工作正常。

    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);

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM