Skip to Content

Archived discussions are read-only. Learn more about SAP Q&A

BO SDK和RESTful web service的集成问题

公司JAVA工程师没有接触过RESTful web services和JAVA的集成,由于之前一直使用的是ReportEngine SDK实现TOKEN,超链,权限表查看等,做了JAVA的portal和非定制化的提示页面,需要提取WEBI的prompt,看了相关的许多资料,知道原理是怎么回事,先"POST"TOKEN数据到HTTP的header,然后再GET出WEBI的prompt,相关代码的语法问题不大清楚,想咨询下这个具体是怎么调用应用程序的。求大神赐教。

Former Member
replied

我把主要内容发给你:
这是一个把webi报表导成.csv格式的例子代码。
<%@ page import =  "java.io.BufferedReader" %>;
<%@ page import =  "java.io.BufferedWriter" %>;
<%@ page import =  "java.io.File" %>;
<%@ page import =  "java.io.FileWriter" %>;
<%@ page import =  "java.io.IOException" %>;
<%@ page import =  "java.io.InputStreamReader" %>;
<%@ page import = "java.io.OutputStreamWriter" %>;
<%@ page import =  "java.net.HttpURLConnection" %>;
<%@ page import =  "java.net.MalformedURLException" %>;
<%@ page import =  "java.net.ProtocolException" %>;
<%@ page import =  "java.net.URL" %>;
<%@ page import =  "org.json.simple.JSONObject" %>;
<%@ page import =  "org.json.simple.parser.JSONParser" %>;
<%@ page import =  "org.json.simple.parser.ParseException" %>;
<%
try
{
URL url = new URL("http://localhost:6405/biprws/raylight/v1/documents/7485/dataproviders/DP0/flows/0");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("Accept", "text/plain");String logonToken = "\"" + getLogonToken() + "\"";
conn.setRequestProperty("X-SAP-LogonToken", logonToken);
conn.setDoOutput(true);
conn.setDoInput(true);
conn.connect();
if (conn.getResponseCode() != 200) {
  throw new RuntimeException("Failed : HTTP error code : "
    + conn.getResponseCode());
}
BufferedReader br = new BufferedReader(new InputStreamReader(
   (conn.getInputStream())));
String output;
String path = "C://rest.csv"; //Specify the location and name for the exported file to be saved
File f1 = new File(path);f1.createNewFile();
FileWriter fw = new FileWriter(f1);
BufferedWriter bw = new BufferedWriter(fw);
while ((output = br.readLine()) != null) {
  bw.write(output);
  bw.write("\n");
}
bw.close();conn.disconnect();out.println("The report is exported successfully at location " + path);
} catch (MalformedURLException e)
{
e.printStackTrace();
}
catch (ProtocolException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
catch (ParseException e)
{
e.printStackTrace();
}
%>
<%
public static String getLogonToken() throws ParseException, IOException {
    String logontoken = null;   
URL url = new URL("http://localhost:6405/biprws/logon/long/");
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    conn.setRequestMethod("POST");
    conn.setRequestProperty("Accept", "application/json");
    conn.setRequestProperty("Content-Type", "application/xml; charset=utf-8");
    conn.setDoInput(true);
    conn.setDoOutput(true);
    String body = "<attrs xmlns=\"http://www.sap.com/rws/bip\">"
            + "<attr name=\"userName\" type=\"string\">Administrator</attr>"
            + "<attr name=\"password\" type=\"string\">Password1</attr>"
            + "<attr name=\"auth\" type=\"string\" possibilities=\"secEnterprise,secLDAP,secWinAD\">secEnterprise</attr>"
            + "</attrs>";

    int len = body.length();
    conn.setRequestProperty("Content-Length", Integer.toString(len));
    conn.connect();

    OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream());
    out.write(body, 0, len);
    out.flush();
    if (conn.getResponseCode() != 200) {
        throw new RuntimeException("Failed : HTTP error code : "
                + conn.getResponseCode());
    }

    BufferedReader br = new BufferedReader(new InputStreamReader(
            (conn.getInputStream())));
    String jsontxt = br.readLine();
    JSONParser parser = new JSONParser();
    JSONObject json = (JSONObject) parser.parse(jsontxt);
    logontoken = (String) json.get("logonToken");
    conn.disconnect();

    return logontoken;
}
%>

0 View this answer in context

Helpful Answer

by
Not what you were looking for? View more on this topic or Ask a question