将字符串中的Headers数据转换为Map>。

huangapple go评论66阅读模式
英文:

Convert Headers data in String to Map<String, List<String>>

问题

我将HTTP头数据作为以下字符串。

{Accept=[*/*], accept-encoding=[gzip, deflate, br], cache-control=[no-cache], connection=[keep-alive], Content-Length=[273], content-type=[application/xml], host=[localhost:8090], SOAPAction=[&quot;http://someurl&quot;]}

基于 &#39;,&#39; 进行拆分会导致错误的拆分,因为值之间也被 &#39;,&#39; 分隔。我无法将其转换为 Map&lt;String, List&lt;String&gt;&gt;MultivaluedMap&lt;String, String&gt;

英文:

I have the HTTP headers data as a String as below.

{Accept=[*/*], accept-encoding=[gzip, deflate, br], cache-control=[no-cache], connection=[keep-alive], Content-Length=[273], content-type=[application/xml], host=[localhost:8090], SOAPAction=[&quot;http://someurl&quot;]}

Splitting based on &#39;,&#39; leads to incorrect splitting as the values are also separated by &#39;,&#39;. I am not able to convert this to a Map&lt;String, List&lt;String&gt;&gt; or MultivaluedMap&lt;String, String&gt;.

答案1

得分: 1

假设这些值始终在 [ ] 内,您可以使用非贪婪正则表达式来提取标题及其值。然后,只需在逗号处分割值,并将它们添加到您的 Map 中。

String input = "{'Accept'=['*/*'], 'accept-encoding'=['gzip, deflate, br'], 'cache-control'=['no-cache'], 'connection'=['keep-alive'], 'Content-Length'=['273'], 'content-type'=['application/xml'], 'host'=['localhost:8090'], 'SOAPAction'=['\"http://someurl\"']}";

Pattern pattern = Pattern.compile("([-\\w]+)=\\[(.*?)]");
Matcher matcher = pattern.matcher(input);

Map<String, List<String>> map = new HashMap<>();
while (matcher.find()) {
    String key = matcher.group(1);  // the header
    String val = matcher.group(2);  // its value

    map.put(key, Arrays.asList(val.split("\\s,\\s")));
}

System.out.println(map);

输出:

{'SOAPAction'=['"http://someurl"'], 'Accept'=['*/*'], 'host'=['localhost:8090'], 'connection'=['keep-alive'], 'content-type'=['application/xml'], 'cache-control'=['no-cache'], 'Content-Length'=['273'], 'accept-encoding'=['gzip, deflate, br']}
英文:

Assuming the values are always within [ ], you can use a non-greedy regex to extract the headers and their values. Then, just split the values at , and add them to your Map.

String input = &quot;{Accept=[*/*], accept-encoding=[gzip, deflate, br], cache-control=[no-cache], connection=[keep-alive], Content-Length=[273], content-type=[application/xml], host=[localhost:8090], SOAPAction=[\&quot;http://someurl\&quot;]}&quot;;

Pattern pattern = Pattern.compile(&quot;([-\\w]+)=\\[(.*?)]&quot;);
Matcher matcher = pattern.matcher(input);

Map&lt;String, List&lt;String&gt;&gt; map = new HashMap&lt;&gt;();
while (matcher.find()) {
    String key = matcher.group(1);  // the header
    String val = matcher.group(2);  // its value

    map.put(key, Arrays.asList(val.split(&quot;\\s,\\s&quot;))));
}

System.out.println(map);

Output:

{SOAPAction=[&quot;http://someurl&quot;], Accept=[*/*], host=[localhost:8090], connection=[keep-alive], content-type=[application/xml], cache-control=[no-cache], Content-Length=[273], accept-encoding=[gzip, deflate, br]}

huangapple
  • 本文由 发表于 2020年8月20日 02:38:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/63493091.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定