sinat_36820379 2017-11-07 06:33
浏览 331

远程调用webservice neilyooo

package com.hand.demo;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Base64;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.zip.GZIPInputStream;

public class AuctionGenerator {
private static String reportServiceUrl = "https://efcw.bi.ap4.oraclecloud.com/xmlpserver/services/v2/ReportService";
private static String negotiationServiceUrl = "https://efcw.prc.ap4.oraclecloud.com:443/prcPonNegotiations/NegotiationManageServiceV3";
private static String uid = "tn.webservice";
private static String pwd = "Welcome123";

public String httpPost(String destUrl, String postData, String authStr, String soapAction) throws Exception {
    String response = "";

    URL url = new URL(destUrl);
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    if (conn == null) {
        return null;
    }
    conn.setRequestProperty("Content-Type", "text/xml; charset=UTF-8");
    conn.setRequestProperty("SOAPAction", soapAction);
    conn.setDoOutput(true);
    conn.setDoInput(true);
    conn.setUseCaches(false);
    conn.setFollowRedirects(true);
    conn.setAllowUserInteraction(false);
    conn.setRequestMethod("POST");

    System.out.println("==================Service request headers: ================ ");
    Map<String,List<String>> requestHeaders = conn.getRequestProperties();
    for(String k : requestHeaders.keySet()){
        System.out.println(k + ":" + requestHeaders.get(k));
    }

    byte[] authBytes = authStr.getBytes("UTF-8");
    String auth = new String(Base64.getEncoder().encode(authBytes));
    conn.setRequestProperty("Authorization", "Basic " + auth);

    System.out.println("==================Service request body: ================ ");
    System.out.println(postData);

    OutputStream out = conn.getOutputStream();
    OutputStreamWriter writer = new OutputStreamWriter(out, "UTF-8");
    writer.write(postData);
    writer.close();
    out.close();

    System.out.println("==================Service response: ================ ");        
    System.out.println(
            "connection status: " + conn.getResponseCode() + "; connection response: " + conn.getResponseMessage());

    System.out.println("==================Service response headers: ================ ");    
    for (int i = 0;; i++) {
        String mine = conn.getHeaderField(i);
        if (mine == null)
            break;
        System.out.println(conn.getHeaderFieldKey(i) + ":" + mine);
    }

    int respCode = conn.getResponseCode();
    if (respCode != 200) {
        InputStream ei = conn.getErrorStream();

        response = readGzipStr(ei);
        ei.close();

        return response;
    }

    InputStream in = conn.getInputStream();
    InputStreamReader iReader = new InputStreamReader(in);
    BufferedReader bReader = new BufferedReader(iReader);

    String line;

    System.out.println("==================Service response: ================ ");

    while ((line = bReader.readLine()) != null) {
        response += line;
    }
    iReader.close();
    bReader.close();
    in.close();
    conn.disconnect();

    return response;
}

public static String readGzipStr(InputStream in) {
    String str = "";
    GZIPInputStream gunzip = null;
    try {
        gunzip = new GZIPInputStream(in);
        ByteArrayOutputStream out = new ByteArrayOutputStream();

        byte[] buffer = new byte[1024];
        int offset = -1;
        while ((offset = gunzip.read(buffer)) != -1) {
            out.write(buffer, 0, offset);
        }

        str = out.toString();

        out.flush();
        out.close();

    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (gunzip != null) {
            try {
                gunzip.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    return str.toString();
}

public String getPayloadTemplate(String id) {
    StringBuffer sb = new StringBuffer();

    try {
        InputStream tmpIS = AuctionGenerator.class.getResourceAsStream(id);
        String line = null;

        BufferedReader reader = new BufferedReader(new InputStreamReader(tmpIS));
        while ((line = reader.readLine()) != null) {
            sb.append(line);
        }
        reader.close();
        tmpIS.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return sb.toString();
}

public String queryAuctionData(String auctionPrjNum) {
    String payTemplate = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:v2=\"http://xmlns.oracle.com/oxp/service/v2\">"+
    "    <soapenv:Header/>"+
    "    <soapenv:Body>"+
    "        <v2:runReport>"+
    "            <v2:reportRequest>"+
    "                <v2:attributeCalendar>Gregorian</v2:attributeCalendar>"+
    "                <v2:attributeFormat>XML</v2:attributeFormat>"+
    "                <v2:attributeLocale>en_US</v2:attributeLocale>"+
    "                <v2:attributeUILocale>en_US</v2:attributeUILocale>"+
    "                <v2:attributeTimezone>China/Hong_Kong</v2:attributeTimezone>"+
    "                <v2:parameterNameValues>"+
    "                    <v2:listOfParamNameValues>"+
    "                        <v2:item>"+
    "                            <v2:dataType>String</v2:dataType>"+
    "                            <v2:name>P_AUC_PRJ_NUM</v2:name>"+
    "                            <v2:values>"+
    "                                <v2:item>%s</v2:item>"+
    "                            </v2:values>"+
    "                        </v2:item>"+
    "                    </v2:listOfParamNameValues>"+
    "                </v2:parameterNameValues>"+
    "                <v2:reportAbsolutePath>/Custom/Procurement/Custom Interface/XXTN_INT_RPT_I30.xdo</v2:reportAbsolutePath>"+
    "            </v2:reportRequest>"+
    "            <v2:userID>%s</v2:userID>"+
    "            <v2:password>%s</v2:password>"+
    "        </v2:runReport>"+
    "    </soapenv:Body>"+
    "</soapenv:Envelope>";
    String requestPayload = String.format(payTemplate, auctionPrjNum, uid, pwd);
    String response = "";
    String soapAction = "http://xmlns.oracle.com/xmlpserver/services/v2/ReportService";

    try {

        response = httpPost(reportServiceUrl, String.format(requestPayload, auctionPrjNum), uid + ":" + pwd, soapAction);

    } catch (Exception e) {
        e.printStackTrace();
    }

    Pattern p = Pattern.compile("reportBytes>(.*)<\\/reportBytes>");
    Matcher m = p.matcher(response);
    while (m.find()) {
        response = m.group(1);
        response = new String(Base64.getDecoder().decode(response));
    }

    return response;
}

public String createAuction(String payload) {
    String response = "";
    String soapAction = "http://xmlns.oracle.com/apps/prc/pon/negotiations/negotiationsServiceV3/initializeNegotiation";

    try {

        response = httpPost(negotiationServiceUrl, payload, uid + ":" + pwd, soapAction);

    } catch (Exception e) {
        e.printStackTrace();
    }

    Pattern p = Pattern.compile(">(\\d.*)<\\/result>");
    Matcher m = p.matcher(response);
    if (m.find()) {
        response = m.group(1);
    }else{
        p = Pattern.compile("faultstring>(.*)<\\/faultstring>");
        m = p.matcher(response);

        if (m.find()) {
            response = m.group(1);
        }
    }

    return response;
}

public void run(String projectNum) {
    String auctionDatas = queryAuctionData(projectNum);

    String auctionNumber = createAuction(auctionDatas);
    System.out.println(auctionNumber);
}

public static void main(String[] args) {

    AuctionGenerator ag = new AuctionGenerator();
    ag.run("50000065");

}

}

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 fluent的在模拟压强时使用希望得到一些建议
    • ¥15 STM32驱动继电器
    • ¥15 Windows server update services
    • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
    • ¥15 模糊pid与pid仿真结果几乎一样
    • ¥15 java的GUI的运用
    • ¥15 Web.config连不上数据库
    • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
    • ¥15 怎么配置广告联盟瀑布流
    • ¥15 Rstudio 保存代码闪退