1、http 請求json數據 請求頭怎麼寫
Http之Get/Post請求區別 1.HTTP請求格式: [] 在HTTP請求中,第一行必須是一個請求行(request line),用來說明請求類型、要訪問的資源以及使用的HTTP版本。緊接著是一個首部(header)小節,用來說明伺服器要使用的附加信息。
2、content-type application/json 請求 服務端怎麼獲取請求數據?
在Android/java平台上實現POST一個json數據:
JSONObject jsonObj = new JSONObject();
jsonObj.put("username", username);
jsonObj.put("apikey", apikey);
// Create the POST object and add the parameters
HttpPost httpPost = new HttpPost(url);
StringEntity entity = new StringEntity(jsonObj.toString(), HTTP.UTF_8);
entity.setContentType("application/json");
httpPost.setEntity(entity);
HttpClient client = new DefaultHttpClient();
HttpResponse response = client.execute(httpPost);
用curl可執行如下命令:
curl -l -H "Content-type: application/json" -X POST -d '{"phone":"13521389587","password":"test"}' http://domain/apis/users.json
用jQuery:
$.ajax({
url:url,
type:"POST",
data:data,
contentType:"application/json; charset=utf-8",
dataType:"json",
success: function(){
...
}
})
服務端(Server)是為客戶端服務的,服務的內容諸如向客戶端提供資源,保存客戶端數據。一般大型的服務端都是在linux環境下搭建。
服務端不具備運算能力,因為服務端同時會與多個客戶端建立連接,一旦服務端進行運算的話,就會佔用大量的資源,從而影響到其他客戶端的通信。
服務端是一種有針對性的服務程序。它的主要表現形式以「windows窗口程序」與「控制台」為主。一般大型的服務端都是在linux環境下搭建。運行服務端的電腦稱之為「伺服器」。
3、怎麼通過http請求獲取到json數據
得到對方返回的String數據,然後自己做相關轉換就可以了。
這里有HTTP請求整套源碼:
http://www.sojson.com/blog/123.html然後下面是線上的Demo:
在線HTTP POST/GET ... 介面測試工具
http://www.sojson.com/httpRequest/4、如何使用@RequestBody一個JSONP請求
JSONP意味著jQuery將創建一個<腳本> 與元素的src 指向控制器的URL。
正如你所看到的,這種做法不允許你通過任何數據請求體,所有的數據應在URL中傳遞查詢參數。 數據:{ABC:'123'} 表示 ABC = 123 添加到URL
在控制器端需要使用任何 @RequestParam (綁定inidivial參數)或者 @ModelAttribute (以綁定多個參數到一個對象):
公共返回object getBlahBlah(@RequestParam(「ABC」)INT ABC)拋出異常{...}
5、如何使用Alamofire請求一個JSON數據
搭建一個WCF,這個WCF接收一個JSON,並會返回一個JSON,使用安卓實現連接。
使用Swift,連接它的時候,POST傳遞字典格式
代碼如下:
let parameters: Dictionary<String,AnyObject> = ["userId": id,"pwd":psw]
Alamofire.request(.POST,, parameters: parameters).responseJSON { ( request, response, JSON, error) in
println(request)
println(response)
println(JSON)
println(error)
6、怎麼通過JSON發送個Http 請求?
試試這個代碼:Button show_data;JSONObject my_json_obj;String path,firstname,lastname;path = " http://192.168.101.123:255/services/services.php?id=9"; HttpClient client = new DefaultHttpClient(); HttpConnectionParams.setConnectionTimeout(client.getParams(), 10000); HttpEntity entity; HttpResponse response = null; HttpURLConnection urlconn; my_json_obj = new JSONObject(); try { urlconn = (HttpURLConnection) new URL(path).openConnection(); urlconn.setConnectTimeout(10000); urlconn.setDoOutput(true); OutputStreamWriter writer = new OutputStreamWriter(urlconn.getOutputStream(), "UTF-8");my_json_obj.put("sUserName", "test2"); my_json_obj.put("sPassword", "123456");writer.write(my_json_obj.toString()); writer.close();if(true) { String temp; temp = WebRequestCall(my_json_obj); //Log.i("Reply", temp); }
7、如何創建一個json 請求body
一個標準的 JSON 請求的實現過程:HttpPost request = new HttpPost(url);// 先封裝一個 JSON 對象JSONObject param = new JSONObject();param.put("name", "rarnu");param.put("password", "123456");// 綁定到請求 EntryStringEntity se = new StringEntity(param.toString()); request.setEntity(se);// 發送請求HttpResponse httpResponse = new DefaultHttpClient().execute(request);// 得到應答的字元串,這也是一個 JSON 格式保存的數據String retSrc = EntityUtils.toString(httpResponse.getEntity());// 生成 JSON 對象JSONObject result = new JSONObject( retSrc);String token = result.get("token");
8、json在線解析
我之前也遇到這個問題了,現在解決了,代碼給你參考下
//POST方式,需要Authorization,json_post_out()輸出數組形式的數據 $url為請求地址,$data為json數據格式
function json_post_out($url,$data,$auth){
header("Content-type:text/html; charset=utf-8");
$headers['Authorization'] = $auth;
// 參數數組
$ch = curl_init ();
curl_setopt ( $ch, CURLOPT_URL, $url );
curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt ( $ch, CURLOPT_POST, 1 );
//curl_setopt ( $ch, CURLOPT_HEADER, 0 );
curl_setopt ( $ch, CURLOPT_POSTFIELDS, $data );
curl_setopt($ch,CURLOPT_ENCODING,"gzip");//將json數據壓縮 重要!!!!!
$return=curl_exec ( $ch );
curl_close ( $ch );
$json = preg_replace('/HTTP(.*)gzip/is','',$return);//解壓縮 重要!!!!!
$json = json_decode($json);
var_mp($json);
}
9、content-type application/json 請求 服務端怎麼獲取請求數據
在Android/java平台上實現POST一個json數據:
JSONObject jsonObj = new JSONObject();
jsonObj.put("username", username);
jsonObj.put("apikey", apikey);
// Create the POST object and add the parameters
HttpPost httpPost = new HttpPost(url);
StringEntity entity = new StringEntity(jsonObj.toString(), HTTP.UTF_8);
entity.setContentType("application/json");
httpPost.setEntity(entity);
HttpClient client = new DefaultHttpClient();
HttpResponse response = client.execute(httpPost);
用curl可執行如下命令:
curl -l -H "Content-type: application/json" -X POST -d '{"phone":"13521389587","password":"test"}' http://domain/apis/users.json
用jQuery:
$.ajax({
url:url,
type:"POST",
data:data,
contentType:"application/json; charset=utf-8",
dataType:"json",
success: function(){
...
}
})
服務端(Server)是為客戶端服務的,服務的內容諸如向客戶端提供資源,保存客戶端數據。一般大型的服務端都是在linux環境下搭建。
服務端不具備運算能力,因為服務端同時會與多個客戶端建立連接,一旦服務端進行運算的話,就會佔用大量的資源,從而影響到其他客戶端的通信。
服務端是一種有針對性的服務程序。它的主要表現形式以「windows窗口程序」與「控制台」為主。一般大型的服務端都是在linux環境下搭建。運行服務端的電腦稱之為「伺服器」。
10、html5調用json數據介面怎麼寫
用js的ajax,這個是jquery的一個簡單例子,純js的會稍微復雜點,
$.ajax({
type: "post",//請求方式
dataType: "json",//數據類型
url: "",//請求地址
success: function (msg) {
//成功之後調用
}
});