我想輸入請求URL和請求主體爲xml並獲取輸出作爲響應xml代碼。使用Python,POST將XML塊發送到Web服務器
有沒有任何代碼片段。
雖然我在Java中此代碼:
import java.net.*; // import java packages.
import java.io.*;
import java.net.URL;
public class POST {
public static void main(String[] args) {
try{
URL url = new URL(""); // Http request
URLConnection con = url.openConnection();
con.setDoInput(true);
con.setDoOutput(true);
con.setConnectTimeout(20000);
con.setReadTimeout(20000);
con.setUseCaches (false);
con.setDefaultUseCaches (false);
con.setRequestProperty ("Content-Type", "application/xml");
OutputStreamWriter writer = new OutputStreamWriter(con.getOutputStream());
writer.write("xml request body"); // xml request body
writer.flush();
writer.close();
InputStreamReader reader = new InputStreamReader(con.getInputStream());
StringBuilder buf = new StringBuilder();
char[] cbuf = new char[ 2048 ];
int num;
while (-1 != (num=reader.read(cbuf)))
{
buf.append(cbuf, 0, num);
}
String result = buf.toString();
System.err.println("\nResponse from server after POST:\n" + result);
}
catch(Throwable t)
{
t.printStackTrace(System.out);
}
}
}
你有什麼試過嗎?我建議您查看urllib2和/或Python請求。 – pcalcao
根據OP對下面答案的評論添加了'python-3.x'標籤。 – cyroxx