POST請求傳入中文引數,接收端亂碼

2021-10-11 14:34:54 字數 1454 閱讀 3362

追蹤過程:

介面採用post請求進行,無法收取簡訊的介面**如下:

public static string sendpost(string url, mapparammap) 

param = new stringbuilder(param.substring(0, param.length() - 1));

}// 傳送請求引數

out.print(param);

// flush輸出流的緩衝

out.flush();

// 定義bufferedreader輸入流來讀取url的響應

in = new bufferedreader(

new inputstreamreader(conn.getinputstream(), "utf-8"));

string line;

while ((line = in.readline()) != null)

} catch (exception e)

// 使用finally塊來關閉輸出流、輸入流

finally

if (in != null)

} catch (ioexception ex)

}return result;

}

其中的引數parammap如下:

mapparams = new hashmap();

params.put("userid", userid);

params.put("ts", ts);

params.put("sign", md5.tolowercase());

params.put("mobile", phones);

params.put("msgcontent", "【您好!請注意】" + content);

params.put("extnum", sendtermid);

params.put("time", sendtime);

問題的源頭是其中的key 為msgcontent的value中含有中文,而中文內容如果不通過urlencoder.encode(param,"utf-8"),"utf-8")進行編碼設定,那麼會被預設的通過其他編碼格式進行編碼後傳送出去,導致簡訊平台伺服器接收到的簡訊內容是亂碼

解決方案:在入參時進行utf-8編碼處理

urlencoder.encode(urlencoder.encode(param,"utf-8"),"utf-8"))
服務端獲取引數時進行一次解碼

string param= urldecoder.decode(param, "utf-8");
感謝文章:

請求引數接收

今天遇到乙個問題,前台傳乙個物件,該物件中有個屬性,屬性的值是陣列。後台直接用實體物件去接收,接收不到,前台報錯 badrequest404 仔細排查過後,發現應該這麼寫請求 ajax 標紅的是重要的 必須是json格式的內容,而且後台必須加 requestbody註解 不加不報錯,但是實體沒有值,...

接收請求引數

採用基本型別接收請求引數 get post 在action類中定義與請求引數同名的屬性,struts2便能自動接收請求引數並賦予給同名屬性。請求路徑 http localhost 8080 test view.action?id 78 public class productaction publi...

c POST請求和接收post請求

c post請求傳送資料 post請求 請求位址 請求值 編碼格式 如果資料量過大,則建議使用流的方式 方法如下 byte bytes null if stype 2 else bool b postbaitourzratemodel strurl,bytes post資料得到結果 url 引數 s...