Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/391.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/353.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java HttpURLConnection-将xml和url参数作为单独的实体发布_Java - Fatal编程技术网

Java HttpURLConnection-将xml和url参数作为单独的实体发布

Java HttpURLConnection-将xml和url参数作为单独的实体发布,java,Java,现有代码: 发送id并在正文中传入url和xml数据 String url = http://localhost:8080/FirstServlet/MyFirstServletMapping&id=123&pass=sDff HttpURLConnection urlc = (HttpURLConnection) new URL(url).openConnection(); urlc.setDoOutput(true); urlc.setRequestMethod("POST"

现有代码: 发送id并在正文中传入url和xml数据

String url = http://localhost:8080/FirstServlet/MyFirstServletMapping&id=123&pass=sDff
HttpURLConnection urlc = (HttpURLConnection) new URL(url).openConnection();
urlc.setDoOutput(true);
urlc.setRequestMethod("POST");
OutputStream out = urlc.getOutputStream();
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out, "UTF-8"));
writer.write("<xml>");
writer.flush();
writer.close();
字符串url=http://localhost:8080/FirstServlet/MyFirstServletMapping&id=123&pass=sDff
HttpURLConnection urlc=(HttpURLConnection)新URL(URL).openConnection();
urlc.setDoOutput(真);
urlc.setRequestMethod(“POST”);
OutputStream out=urlc.getOutputStream();
BufferedWriter writer=新的BufferedWriter(新的OutputStreamWriter(输出,“UTF-8”));
作者:写(“”);
writer.flush();
writer.close();
现在我需要更改代码,以便在POST中发送id和pass。 如何更改代码以添加id并在POST中传递,并且不中断服务器读取

我试着如下所示,但我看到xml被附加到了过程的末尾。 这会导致服务器端出现任何问题吗?或者这看起来不错?有什么建议吗

新代码:

    String url = "http://localhost:8080/FirstServlet/MyFirstServletMapping";
 HttpURLConnection urlc = (HttpURLConnection) new     URL(url).openConnection();
urlc.setDoOutput(true);
urlc.setRequestMethod("POST");
OutputStream out = urlc.getOutputStream();
byte[] postData = setRequestData(111124l, "password5");
out.write(postData);
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out, "UTF-8"));
writer.write("xml");
writer.flush();
writer.close();

private static byte[] setRequestData(Long prsId, String password) throws IOException {
Map<String, Object> params = new LinkedHashMap<String, Object>();
params.put("id", prsId);
params.put("pass", password);
StringBuilder postData = new StringBuilder();
for (Map.Entry<String, Object> param : params.entrySet()) {
if (postData.length() != 0)
postData.append('&');
try {
postData.append(URLEncoder.encode(param.getKey(), "UTF-8"));
postData.append('=');                          postData.append(URLEncoder.encode(String.valueOf(param.getValue()), "UTF-8"));
} catch (UnsupportedEncodingException e) {
}
}
byte[] postDataBytes = postData.toString().getBytes("UTF-8");
System.out.println("post data is--->"+postData.toString());
return postDataBytes;    
}
stringurl=”http://localhost:8080/FirstServlet/MyFirstServletMapping";
HttpURLConnection urlc=(HttpURLConnection)新URL(URL).openConnection();
urlc.setDoOutput(真);
urlc.setRequestMethod(“POST”);
OutputStream out=urlc.getOutputStream();
字节[]postData=setRequestData(111124l,“密码5”);
out.write(postData);
BufferedWriter writer=新的BufferedWriter(新的OutputStreamWriter(输出,“UTF-8”));
write.write(“xml”);
writer.flush();
writer.close();
私有静态字节[]setRequestData(长prsId,字符串密码)引发IOException{
Map params=新建LinkedHashMap();
参数put(“id”,prsId);
参数put(“通过”,密码);
StringBuilder postData=新建StringBuilder();
对于(Map.Entry参数:params.entrySet()){
如果(postData.length()!=0)
postData.append('&');
试一试{
append(URLEncoder.encode(param.getKey(),“UTF-8”);
postData.append('=');postData.append(urlcoder.encode(String.valueOf(param.getValue()),“UTF-8”);
}捕获(不支持的编码异常e){
}
}
字节[]postDataBytes=postData.toString().getBytes(“UTF-8”);
System.out.println(“post数据为-->”+postData.toString());
返回后数据字节;
}
输出: 在位法

dsid-->111124 密码为-->password5xml


送达时间:/FirstServlet:end

的可能重复项似乎是您将postData也作为帖子正文而不是url参数写入的。编辑问题以使其更清楚。请发表评论