HttpURLConnectionの直接呼び出しWebサービスにSOAPメッセージを送信



輸入java.io.BufferedReader;
輸入java.io.InputStreamReader;
インポートにjava.io.OutputStream;
インポートのjava.net.HttpURLConnection;
輸入java.net.InetSocketAddress;
輸入java.net.Proxy;
インポートのjava.net.URL;


パブリッククラスのテスト{


    プライベート文字列testConn(文字列XML){
        文字列urlString = "HTTP://.....WebService.asmx"。
        HttpURLConnectionのhttpConn = NULL;
        = nullのうちのOutputStream。
        文字列returnXml = "";
        System.out.println( "开始.......");
        試す{
            プロキシプロキシ=新しいプロキシ(Proxy.Type.HTTP、新たInetSocketAddress( "IP"、端口));
            
            = httpConn(HttpURLConnectionの)新しい新しいURL(urlString).OpenConnection(プロキシ);
            httpConn.setRequestProperty( "Content-Typeの"、 "テキスト/ XML;のcharset = UTF-8");
            httpConn.setRequestProperty( "soapActionString"、「HTTP: //tempuri.org/_3uffpWS/_3uffpWebService / .... ");
            httpConn.setRequestMethod(" POST「);
            httpConn.setDoOutput(真の);
            httpConn.setDoInput(真の);
            httpConn.connect();
            OUT = httpConn。 getOutputStream(); //出力ストリームオブジェクトを取得
            httpConn.getOutputStream()、ライト(xml.getBytes() );. // 出力ストリームにサーバーのSOAPリクエスト文字ストリームに提出する
            out.flush();
            out.close() ;
            INT = httpConn.getResponseCodeコード()。//サーバーの応答ステータスを取得するために使用
            文字列tempString = NULL;
            StringBufferのSB1 =新しいStringBufferを();
            System.out.println( "コード="コード)。
            IF(コード== HttpURLConnection.HTTP_OK){
                BufferedReaderのリーダー=新しいBufferedReaderの(新しいInputStreamReaderの(httpConn.getInputStream()、 "UTF-8"));
                (!(tempString = reader.readLine())= NULL){一方
                    sb1.append(tempString)。
                }
                IF(!= NULLリーダー){
                    reader.close()。
                }
            }他{
                BufferedReaderのリーダー=新しいBufferedReaderの(新しいInputStreamReaderの(httpConn.getErrorStream()、 "UTF-8"));
                ファイルの終わりがヌルに読み込まれるまで、// 1つのラインに読み出さ
                一方{((reader.readLine tempString =())!= NULL)
                    Sb1.append(tempString);
                }
                IF(!= NULLリーダー){
                    reader.Close( );
                }
            }
            //応答パケット
            returnXml sb1.toString =();
            のSystem.out.println( "エンド.......");
        }キャッチ(例外e){
            e.printStackTrace();
        }
        戻りreturnXml;
    }
    

    パブリック静的無効メイン(文字列[] args){

        "<1.0 \ "エンコーディング= \" UTF-8 \文字列XML = XMLバージョン= \" "?>"
            "<石鹸:封筒のxmlns:XSI = \"のhttp://www.w3.org/2001/XMLSchema-instance \」のxmlns:XSD = \ "のhttp://www.w3.org/2001/XMLSchema \"のxmlns :石鹸= \ "のhttp://schemas.xmlsoap.org/soap/envelope/ \">」
            "<ソープ:ボディ>"
            "<ReturnSmsValidateCodeのxmlns = \"のhttp://tempuri.org / ..... ..S / ... Webサービス\ ">"
            "<MEMBERID>文字列</ MEMBERID>"
            "<validateNumUserInput> 155555555555555 </ validateNumUserInput>"
            "</ ReturnSmsValidateCode>"
            "</ SOAP:BODY>"
            「</石鹸:封筒> ";
        
        文字列R =新しいテスト()testConn(XML)。
        システム。out.printlnを(R)
    }
}

元の記事公開 ウォンの賞賛2 ビュー10000 +

おすすめ

転載: blog.csdn.net/u011927449/article/details/104032930