프로그래머의 httpclient---------------httpurlconnection의post 연결 action

2957 단어
1. 앞말.
제목과 같다.
2. 코드.
HttpURLconnection 연결 servlet 예, servlet도 봉인된 액션일 수 있으며, Request를 찾으면 됩니다.
  

    public static void main(String[] args) throws IOException {  
            String r = AnalysisXML.getXml(); //    xml     
            String path ="http://localhost:8080/axis/services/bxserver";  
            java.net.URL url = new java.net.URL(path);    
            //       
            HttpURLConnection connection = (HttpURLConnection) url  
                    .openConnection();  
            //      connection  ,     post  ,       
            // http   ,      true  
            connection.setDoOutput(true);  
            connection.setDoInput(true);  
            connection.setRequestMethod("POST");  
            // Post           
            connection.setUseCaches(false);  
            connection.setInstanceFollowRedirects(true);  
            connection.setRequestProperty("Content-Type",  
                    "text/xml");  
            connection.connect();   
            DataOutputStream out = new DataOutputStream(connection  
                    .getOutputStream());  
            out.writeBytes(r);   
            out.flush();    
            out.close();  
            BufferedReader reader = new BufferedReader(new InputStreamReader(  
                    connection.getInputStream()));  
            String line;  
            System.out.println("=============================");  
            System.out.println("Contents of post request");  
            System.out.println("=============================");  
            while ((line = reader.readLine()) != null) {  
                System.out.println(line);  
            }  
            System.out.println("=============================");  
            System.out.println("Contents of post request ends");  
            System.out.println("=============================");  
            reader.close();  
            connection.disconnect();  
        }  

Servlet      :

[java] view plaincopyprint? CODE              

    String resultXml = "";  
            boolean resultStr = true;  
            String XMLData = null;  
            StringBuffer tempStringBuffer = new StringBuffer();  
            String tempString = null;   
            BufferedReader reader = request.getReader();  
            while ((tempString = reader.readLine()) != null){  
                tempStringBuffer.append(tempString);  
            }   
            XMLData = tempStringBuffer.toString();  


출처:http://blog.csdn.net/hzw2312/article/details/17754811

좋은 웹페이지 즐겨찾기