簡體   English   中英

從URL(Web服務器上的XML文件)獲取輸入流時出錯

[英]Error getting the inputstream from an URL (XML file on the web server)

我正在嘗試獲取一個xml文件作為流,以在此之后對其進行解析。 但我收到此錯誤:

錯誤:缺少參數,操作:,objectType:,objectTable:,objectId:

出現此錯誤,xml文件類似於:

    <ERROR>
    <STATUS>ko</STATUS>
    <OBJECT_TYPE/>
    <OBJECT_TABLE/>
    <OBJECT_ID/>
    <APP_PATH/>
    <FILE_CONTENT/>
    <FILE_EXTENSION/>
    <ERROR>
    missing parameters, action:editObject, objectType:, objectTable:, objectId:
    </ERROR>
    <END_MESSAGE/>
    </ERROR>

但是,當我在瀏覽器上訪問實際鏈接時,得到的文件正確:

    <SUCCESS>
    <STATUS>ok</STATUS>
    <OBJECT_TYPE>resource</OBJECT_TYPE>
    <OBJECT_TABLE>res_version_business</OBJECT_TABLE>
    <OBJECT_ID>206</OBJECT_ID>
    <APP_PATH>start</APP_PATH>
    <FILE_CONTENT>UEsDBBQABgAIAAAAIQA5en1zywEAAGMIAAATAAgCW0NvbnRlbnRfVHlwZXNdLnhtbCCiBAIooAACAAAA</FILE_CONTENT>
    <FILE_EXTENSION>docx</FILE_EXTENSION>
    <ERROR/>
    <END_MESSAGE/>
    </SUCCESS>

這是我的代碼:

    public void sendHttpRequest(String theUrl, String postRequest) throws Exception {

        URL UrlOpenRequest = new URL(theUrl);
        System.out.println("UrlOpenRequest: "+ theUrl);
        HttpURLConnection HttpOpenRequest =  (HttpURLConnection) 
        UrlOpenRequest.openConnection();
        HttpOpenRequest.addRequestProperty("Accept", "*/xml");
        //HttpOpenRequest.setDoInput(true);
        HttpOpenRequest.setDoOutput(true);
        HttpOpenRequest.setRequestMethod("POST");
        HttpOpenRequest.setRequestProperty("Accept", "*/xml");  

        if (!"none".equals(postRequest)) {
            OutputStreamWriter writer = new 
            OutputStreamWriter(HttpOpenRequest.getOutputStream());
            writer.write("fileContent=" + this.fileContentTosend + "&fileExtension=" + this.fileExtension);
            writer.flush();
        } else {
            OutputStreamWriter writer = new 
            OutputStreamWriter(HttpOpenRequest.getOutputStream());
            writer.write("foo=bar");
            writer.flush();
        }
        System.out.println("INPUT STREAM: "+HttpOpenRequest.getInputStream().available());

        this.parse_xml(HttpOpenRequest.getInputStream());
        HttpOpenRequest.disconnect();
    }

關於此錯誤的原因有任何想法嗎?

試試下面的代碼,看看會得到什么:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;


public class Main
{
  public static void main(String[] args)
  throws Exception
  {
    new Main();
  }

  public Main()
  {
    try
    {
      String myUrl = "http://google.com/"; // change your url here

      String results = doHttpUrlConnectionAction(myUrl);
      System.out.println(results);
    }
    catch (Exception e)
    {
      //whatever error
    }
  }

  private String doHttpUrlConnectionAction(String desiredUrl)
  throws Exception
  {
    URL url = null;
    BufferedReader reader = null;
    StringBuilder stringBuilder;

    try
    {
      url = new URL(desiredUrl);
      HttpURLConnection connection = (HttpURLConnection) url.openConnection();
      connection.setRequestMethod("GET");

      connection.setReadTimeout(30*1000);
      connection.connect();

      reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
      stringBuilder = new StringBuilder();

      String line = null;
      while ((line = reader.readLine()) != null)
      {
        stringBuilder.append(line + "\n");
      }
      return stringBuilder.toString();
    }
    catch (Exception e)
    {
      e.printStackTrace();
      throw e;
    }
    finally
    {
      if (reader != null)
      {
        try
        {
          reader.close();
        }
        catch (IOException ioe)
        {
          ioe.printStackTrace();
        }
      }
    }
  }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM