sinat_36820379
sinat_36820379
2017-11-07 06:33

远程调用webservice neilyooo

  • java

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条回答

为你推荐

换一换