URL中带有‘+’等特殊字符

本文详细解析了在前后端交互中,如何处理带有特殊字符如‘+’的URL参数,通过示例代码展示了如何将这些字符转换为服务器可识别的形式,避免了因URL编码问题导致的数据传输错误。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

做项目时遇到个问题,我前台给后台传参数时带了‘+’的参数,后台无法识别。

//添加OnSelect事件
		$("#materialCode").combobox({
	        onSelect: function () {
	            var materialCode = $("#materialCode").combobox('getText')
	            var indexOf = materialCode.indexOf("+");
        		if (indexOf != -1) {
        			materialCode = materialCode.replace("+", "%2B");
        		}
           	 	$('#big1').combobox('clear'); 
            	$('#big1').combobox('reload', 'MPlist.do?materialCode='+materialCode);    
	        }
	    })

查了一下,可以将这些字符转化成服务器可以识别的字符,对应关系如下:
URL字符转义

用其它字符替代吧,或用全角的。

+    URL 中+号表示空格                                 %2B   
空格 URL中的空格可以用+号或者编码           %20 
/   分隔目录和子目录                                     %2F     
?    分隔实际的URL和参数                             %3F     
%    指定特殊字符                                          %25     
#    表示书签                                                  %23     
&    URL 中指定的参数间的分隔符                  %26     
=    URL 中指定参数的值                                %3D

 

篇幅不够,图片来凑。哈哈哈

在Java中发送POST请求时,如果URL中包含`%`符号,可能会导致URL解析错误,因为`%`在URL中是特殊字符,用于表示十六进制编码。为了解决这个问题,你需要对URL中的`%`进行URL编码。 以下是几种处理方法: ### 方法一:使用`java.net.URLEncoder`进行编码 ```java import java.io.UnsupportedEncodingException; import java.net.URLEncoder; import java.net.HttpURLConnection; import java.net.URL; public class PostExample { public static void main(String[] args) throws Exception { String url = "http://example.com/api?param1=value1%value2"; try { url = URLEncoder.encode(url, "UTF-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } URL obj = new URL(url); HttpURLConnection con = (HttpURLConnection) obj.openConnection(); con.setRequestMethod("POST"); // Add request headers con.setRequestProperty("User-Agent", "Mozilla/5.0"); con.setRequestProperty("Accept-Language", "en-US,en;q=0.5"); // Send post request con.setDoOutput(true); int responseCode = con.getResponseCode(); System.out.println("Response Code : " + responseCode); } } ``` ### 方法二:使用`java.net.URL`构造函数 ```java import java.net.URL; import java.net.HttpURLConnection; public class PostExample { public static void main(String[] args) throws Exception { String url = "http://example.com/api?param1=value1%value2"; URL obj = new URL(url); HttpURLConnection con = (HttpURLConnection) obj.openConnection(); con.setRequestMethod("POST"); // Add request headers con.setRequestProperty("User-Agent", "Mozilla/5.0"); con.setRequestProperty("Accept-Language", "en-US,en;q=0.5"); // Send post request con.setDoOutput(true); int responseCode = con.getResponseCode(); System.out.println("Response Code : " + responseCode); } } ``` ### 方法三:使用第三方库(如Apache HttpClient) ```java import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; public class PostExample { public static void main(String[] args) throws Exception { String url = "http://example.com/api?param1=value1%value2"; CloseableHttpClient httpClient = HttpClients.createDefault(); HttpPost httpPost = new HttpPost(url); // Add headers httpPost.setHeader("User-Agent", "Mozilla/5.0"); httpPost.setHeader("Accept-Language", "en-US,en;q=0.5"); // Add body String json = "{\"key\":\"value\"}"; StringEntity entity = new StringEntity(json); httpPost.setEntity(entity); // Execute request CloseableHttpResponse response = httpClient.execute(httpPost); System.out.println("Response Code : " + response.getStatusLine().getStatusCode()); // Close resources response.close(); httpClient.close(); } } ``` ### 总结 1. 使用`URLEncoder`进行URL编码。 2. 使用`java.net.URL`构造函数。 3. 使用第三方库(如Apache HttpClient)。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值