如何使用Java通過POST方法將數據傳遞到HTTP接口?
這是core Java 2 \ x0d \ urlConnectionConnection = URL . open connection()的壹個示例;//url是http服務器地址\ x0d \ connection . setdooutput(true);\ x0d \ PrintWriterout \ x0d \ = new printwriter(connection . get output stream());//獲取輸出流\x0d\//將參數傳遞給服務器\ x0d \ Enumeration enum = name value pairs。keys();\ x0d \ while(enum . hasmorelements())\ x0d \ { String name =(String)enum . next element();\ x0d \ string value = name value pairs . getproperty(name);\ x0d \ charch\ x0d \ if(enum . hasmorelements())ch =‘& amp;‘;elsech =‘\ n‘;\ x0d \ out . print(name+“=“\ x0d \+URL encoder . encode(value)+ch);\x0d\System.out.println(名稱+值);\ x0d \ } \ x0d \ \ x0d \ out . close();\x0d\//獲取輸入流\ x0d \ BufferedReaderin\ x0d \ try \ x0d \ { in = newBufferedReader(new \ x0d \ InputStreamReader(connection . getinputstream()));\ x0d \ } \ x0d \ catch(FileNotFoundExceptionexception)\ x0d \ { InputStreamerr \ x0d \ =((HttpURLConnection)連接)。get errorstream();\ x0d \ if(err = = null)throw exception;\ x0d \ in = newBufferedReader(new inputstreamreader(err));\ x0d \ } \ x0d \ string buffer response = newstring buffer();\ x0d \ Stringline\x0d\//讀取數據\ x0d \ while(line = in . readline())!= null)\ x0d \ response . append(line+“\ n“);\ x0d \ \ x0d \ in . close();\ x0d \ return response . tostring();\x0d\}\x0d\}