如何通过Jsoup将http请求(xml数据)发布到服务器

时间:2017-11-16 03:43:31

标签: java xml http request jsoup

我希望将http请求发布到服务器。我更喜欢在Java中使用Jsoup。

以下是我在Charles Proxy中捕获的示例原始数据。

任何人都可以帮我在JSoup中构建http post格式。

数据采用xml格式,如下所示。

POST /servlet/PracticeGameServlet?slotId=4785973322575773697&userId=1809015101&gameName=farm_queen&slotType=light HTTP/1.1
Host: www.luogames.com
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:45.0) Gecko/20100101 Firefox/45.0
Accept: application/xml, text/xml, */*; q=0.01
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Content-Type: text/xml; charset=UTF-8
X-Requested-With: XMLHttpRequest
Referer: http://www.luogames.com/games/puzzle-games/farm-queen/?action=play&templateId=9907&accepted=true
Content-Length: 858
Cookie: combo=pref_lang=en_US:partner=midas:user=g_gabr:country=US:type=0:operator=0:version=3.0:userId=1809015109:checksum=a2b4276039dda3324aeb2e75451c7e6c; __utma=141518769.969069584.1510752477.1510800193.1510802396.4; __utmz=141518769.1510752477.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); __utmv=141518769.|1=partner=midas=1^2=userType=practice_player=1^3=isVIP=false=1; OU812=6; JSESSIONID=hdkaOGaixpzxmf1nV1c-v; BIGipServercfgsync~p_www.royalgames.com=2115312300.20480.0000; __utmb=141518769.8.9.1510802547163; __utmc=141518769; __utmt=1; l_inst=web170; ver_usr=1809015101_1118652028; midas_test=test
Connection: keep-alive

<?xml version="1.0" encoding="UTF-8" ?>
<client action="gameover" slotId="4785973322575773697" playId="0" request="3" magic="1707974221">
<gameover time="2017-11-15 19:23:16" lastplaydataid="8">
<result><![CDATA[2000]]></result>
<entry id="1" time="2017-11-15 19:22:45"><![CDATA[113|0|0|2000|0]]></entry>
<entry id="2" time="2017-11-15 19:23:07"><![CDATA[111|664|20|0|0|potato]]></entry>
<entry id="3" time="2017-11-15 19:23:07"><![CDATA[112|664|0|0|0]]></entry>
<entry id="4" time="2017-11-15 19:23:07"><![CDATA[101|0|0|0|0]]></entry>
<entry id="5" time="2017-11-15 19:23:07"><![CDATA[103|665|0|0|10]]></entry>
<entry id="6" time="2017-11-15 19:23:16"><![CDATA[2|938|738|580]]></entry>
<entry id="7" time="2017-11-15 19:23:16"><![CDATA[110|0|0|2000|0]]></entry>
<entry id="8" time="2017-11-15 19:23:16"><![CDATA[109|0|1|1|0]]></entry>
</gameover>
</client>

1 个答案:

答案 0 :(得分:0)

如果您想发送带有自定义正文(xml)的http请求,您可以使用:

Response r = Jsoup.connect(host).requestBody(body).method(Method.POST).execute();

如果您想上传xml文件,可以使用:

Response r = Jsoup.connect(host + path).data(key, filename, inputStream).method(Method.POST).execute();

如果您的回复不是html,例如它是xml,则必须使用.ignoreContentType(true)

我希望这对你有帮助