postjava參數
① java HttpPost傳入參數中文亂碼
以上的2個方法最好都要用上 過濾器只能解決POST請求 ,要處理GET請求就要用
bytes = string.getBytes("iso-8859-1") 得到原始的位元組串,再用 string = new String(bytes, "GB2312") 重新得到正確的字元串 。
這個方法,所以最好2個都要寫,這樣不管是POST還是GET請求就都能解決了。
② 怎麼用java程序以post方式發送表單參數給伺服器
POST方式發送請求示例:
Stringfullurl=url;
//打開連接
URLConnectionconn=newURL(fullurl).openConnection();
//設置通用的請求屬性
conn.setRequestProperty("accept","*/*");
conn.setRequestProperty("connection","Keep-Alive");
conn.setRequestProperty("user-agent","Mozilla/5.0(WindowsNT6.1;WOW64)AppleWebKit/537.36(KHTML,likeGecko)Chrome/42.0.2311.90Safari/537.36");
//發送POST請求必須設置下面兩行
conn.setDoInput(true);
conn.setDoOutput(true);
try(
//獲取URLConnection對象對應的輸出流
PrintWriterout=newPrintWriter(conn.getOutputStream());){
out.print(parm);//發送請求參數(key1=value1&key2=value2)
out.flush();//flush輸出流的緩沖
}catch(Exceptione){}
//獲取響應頭欄位
Map<String,List<String>>map=conn.getHeaderFields();
//根據輸入流讀取響應數據
InputStreamis=conn.getInputStream();
僅供參考。
③ java 測試post請求 在body裡面傳遞參數怎麼設置,怎麼接收
第一:jsp就是servlet。。。
第二:頁面向後台傳值有兩種:
a。同步
b。非同步
同步調用就簡單了,action直接就可以。
非同步調用就是用ajax技術,要看你項目里用的是什麼框架。
比如,struts2.1就支持好多。
這些都要根據你的開發環境。
④ 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();
}
(4)postjava參數擴展閱讀:
關於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方法將參數傳遞給第三方網站
使用org.apache.commons.httpclient方便,效率又高,下面是post方式提交登錄參數的代碼:
public class FormLoginDemo
{
static final String LOGON_SITE = "developer.java.sun.com";
static final int LOGON_PORT = 80;
public FormLoginDemo() {
super();
}
public static void main(String[] args) throws Exception {
HttpClient client = new HttpClient();
client.getHostConfiguration().setHost(LOGON_SITE, LOGON_PORT, "http");
PostMethod authpost = new PostMethod("/servlet/SessionServlet");
// 准備登錄參數
NameValuePair action = new NameValuePair("action", "login");
NameValuePair url = new NameValuePair("url", "/index.html");
NameValuePair userid = new NameValuePair("UserId", "userid");
NameValuePair password = new NameValuePair("Password", "password");
authpost.setRequestBody(
new NameValuePair[] {action, url, userid, password});
// 執行Post請求
client.executeMethod(authpost);
// 列印狀態碼
System.out.println("Login form post: " + authpost.getStatusLine().toString());
// 釋放連接
authpost.releaseConnection();
}
}
⑥ Java sendPost請求方法如何加入參數
/**
* 向指定 URL 發送POST方法的請求
*
* @param url
* 發送請求的 URL
* @param param
* 請求參數,請求參數應該是 name1=value1&name2=value2 的形式。
* @return 所代表遠程資源的響應結果
*/
public static String sendPost(String url, String param) {
PrintWriter out = null;
BufferedReader in = null;
String result = "";
try {
URL realUrl = new URL(url);
// 打開和URL之間的連接
URLConnection conn = realUrl.openConnection();
// 設置通用的請求屬性
conn.setRequestProperty("accept", "*/*");
conn.setRequestProperty("connection", "Keep-Alive");
conn.setRequestProperty("user-agent",
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
// 發送POST請求必須設置如下兩行
conn.setDoOutput(true);
conn.setDoInput(true);
// 獲取URLConnection對象對應的輸出流
out = new PrintWriter(conn.getOutputStream());
// 發送請求參數
out.print(param);
// flush輸出流的緩沖
out.flush();
// 定義BufferedReader輸入流來讀取URL的響應
in = new BufferedReader(
new InputStreamReader(conn.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
result += line;
}
} catch (Exception e) {
System.out.println("發送 POST 請求出現異常!"+e);
e.printStackTrace();
}
//使用finally塊來關閉輸出流、輸入流
finally{
try{
if(out!=null){
out.close();
}
if(in!=null){
in.close();
}
}
catch(IOException ex){
ex.printStackTrace();
}
}
return result;
}
⑦ java get和post獲取參數的區別
get是從伺服器上獲取數據,post是向伺服器傳送數據。
對於get方式,伺服器端用Request.QueryString獲取變數的值,對於post方式,伺服器端用Request.Form獲取提交的數據。
GET方式提交的數據最多隻能有1024位元組,而POST則沒有此限制。
安全性問題。
正如在中提到,使用Get的時候,參數會顯示在地址欄上,而Post不會。所以,如果這些數據是中文數據而且是非敏感數據,那麼使用get;如果用戶輸入的數據不是中文字元而且包含敏感數據,那麼還是使用post為好。
在客戶端,Get方式在通過URL提交數據,數據在URL中可以看到;POST方式,數據放置在HTMLHEADER內提交。
⑧ java post請求參數怎麼寫
//serverURL url地址
HttpPost httpPost = new HttpPost(serverURL);
//param 為參數
StringEntity entity = new StringEntity(param);
entity.setContentType("application/x-www-form-urlencoded");
httpPost.setEntity(entity);
HttpResponse httpResponse = httpClient.execute(httpPost);
還可以用map作為參數
List<NameValuePair> formparams = new ArrayList<NameValuePair>();
if(param!=null){
Set set = param.keySet();
Iterator iterator = set.iterator();
while (iterator.hasNext()) {
Object key = iterator.next();
Object value = param.get(key);
formparams.add(new BasicNameValuePair(key.toString(), value.toString()));
}
}
⑨ java post請求問號攜帶參數跟表單里的參數有什麼不一樣嗎
什麼東西?不是很明白你要問的。
請求一般有兩種,post、get
post:參數在requestBody裡面,你也可以強行放在請求鏈接後面
get:參數一般在請求鏈接後面,後端獲取的時候用requestparams,也可以不寫
表單提交,其實就是參數封裝在表單裡面,然後再提交,是一種方便快捷的提交方法。對參數不會有什麼影響。
怎麼處理參數,最重要的還是要看框架,框架的不同,後端獲取參數的寫法一般也有所不同
⑩ java怎麼發送post請求參數
/**
* 向指定 URL 發送POST方法的請求
*
* @param url
* 發送請求的 URL
* @param param
* 請求參數,請求參數應該是 name1=value1&name2=value2 的形式。
* @return 所代表遠程資源的響應結果
*/
public static String sendPost(String url, String param) {
PrintWriter out = null;
BufferedReader in = null;
String result = "";
try {
URL realUrl = new URL(url);
// 打開和URL之間的連接
URLConnection conn = realUrl.openConnection();
// 設置通用的請求屬性
conn.setRequestProperty("accept", "*/*");
conn.setRequestProperty("connection", "Keep-Alive");
conn.setRequestProperty("user-agent",
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
// 發送POST請求必須設置如下兩行
conn.setDoOutput(true);
conn.setDoInput(true);
// 獲取URLConnection對象對應的輸出流
out = new PrintWriter(conn.getOutputStream());
// 發送請求參數
out.print(param);
// flush輸出流的緩沖
out.flush();
// 定義BufferedReader輸入流來讀取URL的響應
in = new BufferedReader(
new InputStreamReader(conn.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
result += line;
}
} catch (Exception e) {
System.out.println("發送 POST 請求出現異常!"+e);
e.printStackTrace();
}
//使用finally塊來關閉輸出流、輸入流
finally{
try{
if(out!=null){
out.close();
}
if(in!=null){
in.close();
}
}
catch(IOException ex){
ex.printStackTrace();
}
}
return result;
}