我想在使用jsoup发送请求时调用一个只接受原始数据的API。
我的代码如下所示:
Document res = Jsoup.connect(url)
.header("Accept", "application/json")
.header("X-Requested-With", "XMLHttpRequest")
.data("name", "test", "room", "bedroom")
.post();
但我知道上面的代码不适合传递原始数据。
有人能告诉我怎么办?
答案 0 :(得分:0)
您必须使用HttpUrlConnection conn = new URL("http://somesite.com/").openConnection();
String str = "some string goes here"; //use a String for our raw data
byte[] outputInBytes = str.getBytes("UTF-8"); //byte array with raw data
OutputStream os = conn.getOutputStream();
os.write( outputInBytes );
将原始数据发布到网站,并以字符串形式获取响应。一些例子,发布数据:
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
StringBuilder response = new StringBuilder();
String inputLine;
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
conn.disconnect();
以字符串形式获取响应:
Document doc = Jsoup.parse(response.toString());
用Jsoup解析:
#include <string>
#include <unordered_map>
class A{
};
int main(int argc, char* argv[]){
std::unordered_map<std::string, std::reference_wrapper<const A>> stringToRef;
A a;
const A& b = a;
stringToRef.insert(std::make_pair("Test", b));
return 0;
}