用Java发送POST数据
我正在尝试用POST从Java提交表单。我想发送一些值并将它们存储在Web服务器上的sql db中。我在堆栈中找到了这个示例,但我没有正确理解它 URL是否应该引用接受POST请求的php文件?我还需要考虑其他的价值观。我使用的参数没有问题,它们应该与我的PHP文件上的POST检查相匹配 这就是成功地将POST数据从Java发送到我的Web服务器所需的全部吗用Java发送POST数据,java,php,Java,Php,我正在尝试用POST从Java提交表单。我想发送一些值并将它们存储在Web服务器上的sql db中。我在堆栈中找到了这个示例,但我没有正确理解它 URL是否应该引用接受POST请求的php文件?我还需要考虑其他的价值观。我使用的参数没有问题,它们应该与我的PHP文件上的POST检查相匹配 这就是成功地将POST数据从Java发送到我的Web服务器所需的全部吗 URL url = new URL("http://g.php"); Map<String,Object> p
URL url = new URL("http://g.php");
Map<String,Object> params = new LinkedHashMap<>();
params.put("value", 5);
params.put("id", 17);
StringBuilder postData = new StringBuilder();
for (Map.Entry<String,Object> param : params.entrySet()) {
if (postData.length() != 0) postData.append('&');
postData.append(URLEncoder.encode(param.getKey(), "UTF-8"));
postData.append('=');
postData.append(URLEncoder.encode(String.valueOf(param.getValue()), "UTF-8"));
}
byte[] postDataBytes = postData.toString().getBytes("UTF-8");
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conn.setRequestProperty("Content-Length", String.valueOf(postDataBytes.length));
conn.setDoOutput(true);
conn.getOutputStream().write(postDataBytes);
Reader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
for (int c; (c = in.read()) >= 0;)
System.out.print((char)c);
URL=新URL(“http://g.php");
Map params=新建LinkedHashMap();
参数put(“值”,5);
参数put(“id”,17);
StringBuilder postData=新建StringBuilder();
对于(Map.Entry参数:params.entrySet()){
如果(postData.length()!=0)postData.append('&');
append(URLEncoder.encode(param.getKey(),“UTF-8”);
postData.append('=');
append(URLEncoder.encode(String.valueOf(param.getValue()),“UTF-8”);
}
字节[]postDataBytes=postData.toString().getBytes(“UTF-8”);
HttpURLConnection conn=(HttpURLConnection)url.openConnection();
conn.setRequestMethod(“POST”);
conn.setRequestProperty(“内容类型”、“应用程序/x-www-form-urlencoded”);
conn.setRequestProperty(“内容长度”,String.valueOf(postDataBytes.Length));
连接设置输出(真);
conn.getOutputStream().write(postDataBytes);
Reader in=新的BufferedReader(新的InputStreamReader(conn.getInputStream(),“UTF-8”);
对于(int c;(c=in.read())>=0;)
系统输出打印((字符)c);
相同,但有注释:
URL url = new URL("http://g.php"); // URL to your application
Map<String,Object> params = new LinkedHashMap<>();
params.put("value", 5); // All parameters, also easy
params.put("id", 17);
StringBuilder postData = new StringBuilder();
// POST as urlencoded is basically key-value pairs, as with GET
// This creates key=value&key=value&... pairs
for (Map.Entry<String,Object> param : params.entrySet()) {
if (postData.length() != 0) postData.append('&');
postData.append(URLEncoder.encode(param.getKey(), "UTF-8"));
postData.append('=');
postData.append(URLEncoder.encode(String.valueOf(param.getValue()), "UTF-8"));
}
// Convert string to byte array, as it should be sent
byte[] postDataBytes = postData.toString().getBytes("UTF-8");
// Connect, easy
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
// Tell server that this is POST and in which format is the data
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conn.setRequestProperty("Content-Length", String.valueOf(postDataBytes.length));
conn.setDoOutput(true);
conn.getOutputStream().write(postDataBytes);
// This gets the output from your server
Reader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
for (int c; (c = in.read()) >= 0;)
System.out.print((char)c);
URL=新URL(“http://g.php"); // 应用程序的URL
Map params=新建LinkedHashMap();
参数put(“值”,5);//所有参数,也很简单
参数put(“id”,17);
StringBuilder postData=新建StringBuilder();
//POST as urlencoded基本上是键值对,与GET一样
//这将创建key=value&key=value&。。。对
对于(Map.Entry参数:params.entrySet()){
如果(postData.length()!=0)postData.append('&');
append(URLEncoder.encode(param.getKey(),“UTF-8”);
postData.append('=');
append(URLEncoder.encode(String.valueOf(param.getValue()),“UTF-8”);
}
//将字符串转换为应发送的字节数组
字节[]postDataBytes=postData.toString().getBytes(“UTF-8”);
//连接,轻松
HttpURLConnection conn=(HttpURLConnection)url.openConnection();
//告诉服务器这是POST,数据的格式是什么
conn.setRequestMethod(“POST”);
conn.setRequestProperty(“内容类型”、“应用程序/x-www-form-urlencoded”);
conn.setRequestProperty(“内容长度”,String.valueOf(postDataBytes.Length));
连接设置输出(真);
conn.getOutputStream().write(postDataBytes);
//这将从服务器获取输出
Reader in=新的BufferedReader(新的InputStreamReader(conn.getInputStream(),“UTF-8”);
对于(int c;(c=in.read())>=0;)
系统输出打印((字符)c);
下面的例子对我很有用
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("http://example.com/");
// Request parameters and other properties.
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("user", "Bob"));
try {
httpPost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
} catch (UnsupportedEncodingException e) {
// writing error to Log
e.printStackTrace();
}
HttpClient-HttpClient=newdefaulthttpclient();
HttpPost HttpPost=新的HttpPost(“http://example.com/");
//请求参数和其他属性。
List params=new ArrayList();
添加参数(新的BasicNameValuePair(“用户”、“Bob”);
试一试{
setEntity(新的UrlEncodedFormEntity(参数,“UTF-8”);
}捕获(不支持的编码异常e){
//将错误写入日志
e、 printStackTrace();
}
Java不是首字母缩略词。已修复。谢谢你的观察谢谢。在php中,我这样检查:if(!empty($\u POST['value']&&&$\u POST['id']){这是否正确?在通过java处理POST时?是的,这与它是否来自java无关。但是您应该分别对每个值使用empty():!empty($\u POST['value'])&!emoty($\u POST['id'])我想指出的是,php有自己的方法来实现这一点:isset($variable)
虽然这将接受空字符串,但它会检查变量是否存在