0

当我尝试从服务器读取数据时,我收到错误 400:从服务器读取数据时出现错误请求错误。尽管为 localhost URL 运行相同的代码时,它可以正常工作。

这是我的代码:

package xmlComm;

import java.io.*;
import java.net.*;
import java.util.*;
import java.text.*;
import javax.xml.*;
import javax.xml.rpc.Call;

public class SampleXml {

    public String TelXml(String msisdn,String region){
        String temp="";
        System.out.println(region);
        try{


            /*******************PARAMETER DECLARATION AND INITIALIZATION *******************************/
            URL url = null;
            String HOSTNAME=InetAddress.getLocalHost().getHostName().replaceAll("[\\-\\+\\.\\^:,]", "");
            Date TIMESTAMP=new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
            SimpleDateFormat sdf1 = new SimpleDateFormat("yyyyMMddHHmmssSSSS");
            String ORIGIN_TIMESTAMP=sdf1.format(TIMESTAMP);
            String FINAL_DATE=ORIGIN_TIMESTAMP.substring(0, 8)+"T"+ORIGIN_TIMESTAMP.substring(8,10)+":"+ORIGIN_TIMESTAMP.substring(10,12)+":"+ORIGIN_TIMESTAMP.substring(12,14)+"+"+ORIGIN_TIMESTAMP.substring(14,18);
            String TRANSACTION_ID=sdf.format(TIMESTAMP);
            HttpURLConnection urlConn;
            DataOutputStream printout;
            BufferedReader input;   

            /********************************************************************************************/

            url =new URL("server URL");// no problem in URL


            urlConn =(HttpURLConnection)url.openConnection();

            System.out.println(urlConn);
            //urlConn.connect();
            //urlConn.wait(2);
            urlConn.setDoInput(true);
            urlConn.setDoOutput(true);
            urlConn.setUseCaches(false);


            //java.util.Date dt = new java.util.Date();
                //              SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
                //              dateTime=formatter.format(dt);
            //set request method
            urlConn.setRequestMethod("POST");
            //set request type
            //urlConn.setRequestProperty("Content-Type", "application/xml");
            //urlConn.setRequestProperty(key, value)
            StringBuffer buf = new StringBuffer(); 
            buf.append("POST /ABC/1.1\n"+
                    "User-Agent: ABC\n"+
                    "Authorization:Basic ABC\n"+
                    "Content-Type: text/xml\n"+
                    "Content-Length: 706\n\n"+
                    "<?xml version=\"1.0\"encoding=\"ISO-8859-1\"?>\n"+
                    "<methodCall>\n"+
                    "<methodName>MethodNameABC</methodName>\n"+
                    "<params>\n"+
                    "<param>\n"+
                    "<value>\n"+
                    "<struct>\n"+
                    "<member>\n"+
                    "<name>originNodeType</name>\n"+
                    "<value><string>EXT</string></value>\n"+
                    "</member>\n"+
                    "<member>\n"+
                    "<name>originHostName</name>\n"+
                    "<value><string>"+HOSTNAME+"</string></value>\n"+
                    "</member>\n"+
                    "<member>\n"+
                    "<name>originTransactionID</name>\n"+
                    "<value><string>"+TRANSACTION_ID+"</string></value>\n"+
                    "</member>\n"+
                    "<member>\n"+
                    "<name>originTimeStamp</name>\n"+
                    "<value><dateTime.iso8601>"+FINAL_DATE+"</dateTime.iso8601></value>\n"+
                    "</member>\n"+
                    "<member>\n"+
                    "<name>subscriberNumber</name>\n"+
                    "<value><string>"+msisdn+"</string></value>\n"+
                    "</member>\n"+
                    "</struct>\n"+
                    "</value>\n"+
                    "</param>\n"+
                    "</params>\n"+
                    "</methodCall>");


            String Inpt = buf.toString();
            System.out.println("*********************************   Request   ********************************");
            System.out.println(Inpt);
            printout = new DataOutputStream(urlConn.getOutputStream());
            printout.writeBytes(Inpt);
            printout.flush ();
            printout.close ();


            //Get response data.
            input = new BufferedReader(new InputStreamReader(urlConn.getInputStream()));

            String str;

            while((str = input.readLine())!=null){
                str += input.readLine();
                    if(str!=null){
                    temp=str;
                }
            }
            System.out.println("*********************************   Response   ********************************");
            System.out.println(temp);

        }   
        catch (MalformedURLException me{
            System.err.println("MalformedURLException; " + me);
        }
        catch (IOException ioe){
            System.err.println("IOException; " + ioe.getMessage());
        }
        String returnmsg = "abcc";
        return returnmsg;
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        SampleXml Sxml= new SampleXml();
        Sxml.TelXml("1234567890", "City");
    }
}
4

0 回答 0