post提交過去後,信息會變成一串字元串,php通過解析這串字元串後返回結果
㈡ JAVA的get post 區別
Form 中的 get 和 post 方法,在數據傳輸過程中分別對應了 HTTP 協議中的 GET 和 POST 方法。二者主要區別如下:
1)Get 是用來從伺服器上獲得數據,而 Post 是用來向伺服器上傳數據;
2)Get 將表單中數據按照variable=value 的形式,添加到 action 所指向的 URL 後面,並且兩者使用「?」連接,而各個變數之間使用「&」連接;Post 是將表單中的數據放在 form 的數據體中,按照變數和值相對應的方式,傳遞到 action 所指向 URL;
3)Get 是不安全的,因為在傳輸過程,數據被放在請求的 URL 中;Post 的所有操作對用戶來說都是不可見的;
4)Get 傳輸的數據量小,這主要是因為受 URL 長度限制;而 Post 可以傳輸大量的數據,所以在上傳文件只能使用 Post;
5)Get 限制 Form 表單的數據集必須為ASCII 字元,而 Post 支持整個 ISO10646 字元集;
6)Get 是 Form 的默認方法。
㈢ java post 數據,不讓頁面跳轉
有很多種方法:
1.用AJAX模式,這是不刷新頁面提交的最好模式,網上很多完整例子,可以參考一下;
2.如果另一個頁面只有業務處理功能,不需要界面顯示的話,可以先POST數據到此頁面處理數據,處理成功後返回到原頁(此實現方案會刷新頁面)。
㈣ 如何在java中發送post請求
package com.test;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
public class D {
public static void main(String[] args){
List<NameValuePair> nvps= new ArrayList<NameValuePair>();
nvps.add(new BasicNameValuePair("id", "1"));
String url="https://www.hao123.com/";
HttpClient httpClient = null;
String response="";
try {
HttpPost post = new HttpPost(url);
post.setHeader("Connection", "close");
httpClient = new DefaultHttpClient();
post.setEntity(new UrlEncodedFormEntity(nvps));
HttpResponse httpres= httpClient.execute(post);
if (httpres.getStatusLine().getStatusCode() >= 300) {
System.out.println("Request Failed,Code:" + httpres.getStatusLine().getStatusCode() + ",URL:" + url);
}
response = EntityUtils.toString(httpres.getEntity(), "utf-8");
}catch(Exception e){
e.printStackTrace();
}finally{
if(httpClient!=null){
httpClient.getConnectionManager().shutdown();
}
}
System.out.println(response);
}
}
需要httpclient-4.1.3.jar,httpcore-4.1.4.jar和commons-logging-1.1.1.jar
㈤ java HttpPost傳入參數中文亂碼
以上的2個方法最好都要用上 過濾器只能解決POST請求 ,要處理GET請求就要用
bytes = string.getBytes("iso-8859-1") 得到原始的位元組串,再用 string = new String(bytes, "GB2312") 重新得到正確的字元串 。
這個方法,所以最好2個都要寫,這樣不管是POST還是GET請求就都能解決了。
㈥ java post網頁後接收的漢字是unicode字元集 很急 在線等
你是否在windows上跑的呢?如果是的話,你的輸出也需要使用編碼:
///PrintWriterout=newPrintWriter(conn.getOutputStream()));
//增加輸出流的字元編碼
PrintWriterout=newPrintWriter(conn.getOutputStream(),"UTF-8");
如果你的後台使用servlet容器,你還需要在servlet裡面加上。
request.setCharacterEncoding("UTF-8")
㈦ java模擬登錄問題(post請求帶特殊符號問題)
如果你是用java的api實現的模擬post請求,那麼你需要在你之前構造的http request的header里加上
Cookie:名字=值 然後統一包裝成你的conenction的OutputStream。
建議你用apache的HttpClient api項目,裡面有專門處理cookie的api,這樣事情就簡單許多。
㈧ java web post傳值 特殊符號
傳遞前用URLEncoder.encode轉換一下。
hello.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String a = "abc+abc";
%>
<a href="a.jsp?path=<%=java.net.URLEncoder.encode(a,"UTF-8")%>">hello</a>
a.jsp
<%@ page language="java" import="java.util.*,java.net.URLDecoder" pageEncoding="utf-8"%>
<%
String a = new String(request.getParameter("path"));
out.println(a);
%>
結果a.jsp可以正確顯示abc+abc
㈨ java HttpPost怎麼傳遞參數
public class HttpURLConnectionPost {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
readContentFromPost();
}
public static void readContentFromPost() throws IOException {
// Post請求的url,與get不同的是不需要帶參數
URL postUrl = new URL("http://www.xxxxxxx.com");
// 打開連接
HttpURLConnection connection = (HttpURLConnection) postUrl.openConnection();
// 設置是否向connection輸出,因為這個是post請求,參數要放在
// http正文內,因此需要設為true
connection.setDoOutput(true);
// Read from the connection. Default is true.
connection.setDoInput(true);
// 默認是 GET方式
connection.setRequestMethod("POST");
// Post 請求不能使用緩存
connection.setUseCaches(false);
//設置本次連接是否自動重定向
connection.setInstanceFollowRedirects(true);
// 配置本次連接的Content-type,配置為application/x-www-form-urlencoded的
// 意思是正文是urlencoded編碼過的form參數
connection.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
// 連接,從postUrl.openConnection()至此的配置必須要在connect之前完成,
// 要注意的是connection.getOutputStream會隱含的進行connect。
connection.connect();
DataOutputStream out = new DataOutputStream(connection
.getOutputStream());
// 正文,正文內容其實跟get的URL中 '? '後的參數字元串一致
String content = "欄位名=" + URLEncoder.encode("字元串值", "編碼");
// DataOutputStream.writeBytes將字元串中的16位的unicode字元以8位的字元形式寫到流裡面
out.writeBytes(content);
//流用完記得關
out.flush();
out.close();
//獲取響應
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line;
while ((line = reader.readLine()) != null){
System.out.println(line);
}
reader.close();
//該乾的都幹完了,記得把連接斷了
connection.disconnect();
}
關於Java HttpURLConnection使用
public static String sendPostValidate(String serviceUrl, String postData, String userName, String password){
PrintWriter out = null;
BufferedReader in = null;
String result = "";
try {
log.info("POST介面地址:"+serviceUrl);
URL realUrl = new URL(serviceUrl);
// 打開和URL之間的連接
URLConnection conn = realUrl.openConnection();
HttpURLConnection httpUrlConnection = (HttpURLConnection) conn;
// 設置通用的請求屬性
httpUrlConnection.setRequestProperty("accept","*/*");
httpUrlConnection.setRequestProperty("connection", "Keep-Alive");
httpUrlConnection.setRequestProperty("user-agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
httpUrlConnection.setRequestMethod("POST");
httpUrlConnection.setRequestProperty("Content-Type","application/json;charset=UTF-8");
Base64 base64 = new Base64();
String encoded = base64.encodeToString(new String(userName+ ":" +password).getBytes());
httpUrlConnection.setRequestProperty("Authorization", "Basic "+encoded);
// 發送POST請求必須設置如下兩行
httpUrlConnection.setDoOutput(true);
httpUrlConnection.setDoInput(true);
// 獲取URLConnection對象對應的輸出流
out = new PrintWriter(new OutputStreamWriter(httpUrlConnection.getOutputStream(),"utf-8"));
// 發送請求參數
out.print(postData);
out.flush();
// 定義BufferedReader輸入流來讀取URL的響應
in = new BufferedReader(new InputStreamReader(httpUrlConnection.getInputStream(),"utf-8"));
String line;
while ((line = in.readLine()) != null) {
result += line;
}
//
// if (!"".equals(result)) {
// BASE64Decoder decoder = new BASE64Decoder();
// try {
// byte[] b = decoder.decodeBuffer(result);
// result = new String(b, "utf-8");
// } catch (Exception e) {
// e.printStackTrace();
// }
// }
return result;
} catch (Exception e) {
log.info("調用異常",e);
throw new RuntimeException(e);
}
//使用finally塊來關閉輸出流、輸入流
finally{
try{
if(out!=null){
out.close();
}
if(in!=null){
in.close();
}
}
catch(IOException e){
log.info("關閉流異常",e);
}
}
}
}
㈩ java字元串處理,POST獲得網站的返回信息之後將信息存儲在String中,但是怎麼處理字元處理獲得成績姓名,
建議你使用java web的jstl標簽獲取,從網站返回的結果不要放在一個String中,而要把你要顯示的信息存儲到一個list中,在伺服器端放到response.setAttribute()中,頁面使用foreach顯示,可以參考:http://blog.csdn.net/honey_claire/article/details/7664165