簡體   English   中英

如何通過Jsoup將HTTP請求(xml數據)發布到服務器

[英]how to post http request( xml data) to server by Jsoup

我希望將http請求發布到服務器。我更喜歡在Java中使用Jsoup。

以下是我在Charles Proxy中捕獲的樣本原始數據。

誰能幫我在JSoup中構造http帖子格式。

數據為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>

如果要發送帶有自定義正文(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)

希望對您有幫助

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM