我正在尝试使用JSON正文发出HTTP post请求:
如何能够将NSdictionnary添加到HTTP请求主体.
这是我的代码,它似乎无法正常工作.
var entry1 = Response(IdQuestion: 6510,IdProposition: 10,Time: 30)
var entry2 = Response(IdQuestion: 8284,IdProposition: 10,Time: 30)
Responses.append(entry1)
Responses.append(entry2)
let list = Responses.map { $0.asDictionary }
let json = ["List":list,"IdSurvey":"102","IdUser":"iOSclient","UserInformation":"iOSClient"]
let data : NSData = NSKeyedArchiver.archivedDataWithRootObject(json)
NSJSONSerialization.isValidJSONObject(json)
let myURL = NSURL(string: "http://www.myserver.com")!
let request = NSMutableURLRequest(URL: myURL)
request.HTTPMethod = "POST"
request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
request.setValue("application/json", forHTTPHeaderField: "Accept")
request.HTTPBody = data
let task = NSURLSession.sharedSession().dataTaskWithRequest(request) {
data, response, error in
println(response)
// Your completion handler code here
}
task.resume()
Run Code Online (Sandbox Code Playgroud) TL; DR:我是这种语言的新手,不知道我在做什么
到目前为止,这是我的班级:
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Web;
using System.Net;
using System.IO;
public class MyClass
{
private const string URL = "https://sub.domain.com/objects.json?api_key=123";
private const string data = @"{""object"":{""name"":""Title""}}";
public static void CreateObject()
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL);
request.Method = "POST";
request.ContentType = "application/json";
request.ContentLength = data.Length;
StreamWriter requestWriter = new StreamWriter(request.GetRequestStream(), System.Text.Encoding.ASCII);
requestWriter.Write(data);
requestWriter.Close();
try
{
// get the response
WebResponse webResponse = request.GetResponse();
Stream webStream = webResponse.GetResponseStream();
StreamReader responseReader = new StreamReader(webStream);
string response = responseReader.ReadToEnd(); …Run Code Online (Sandbox Code Playgroud) data = {
'ids': [12, 3, 4, 5, 6 , ...]
}
urllib2.urlopen("http://abc.com/api/posts/create",urllib.urlencode(data))
Run Code Online (Sandbox Code Playgroud)
我想发送一个POST请求,但其中一个字段应该是一个数字列表.我怎样才能做到这一点 ?(JSON?)
jQuery v1.7.2
我有这个功能在执行时给我以下错误:
Uncaught TypeError: Illegal invocation
Run Code Online (Sandbox Code Playgroud)
这是功能:
$('form[name="twp-tool-distance-form"]').on('submit', function(e) {
e.preventDefault();
var from = $('form[name="twp-tool-distance-form"] input[name="from"]');
var to = $('form[name="twp-tool-distance-form"] input[name="to"]');
var unit = $('form[name="twp-tool-distance-form"] input[name="unit"]');
var speed = game.unit.speed($(unit).val());
if (!/^\d{3}\|\d{3}$/.test($(from).val()))
{
$(from).css('border-color', 'red');
return false;
}
if (!/^\d{3}\|\d{3}$/.test($(to).val()))
{
$(to).css('border-color', 'red');
return false;
}
var data = {
from : from,
to : to,
speed : speed
};
$.ajax({
url : base_url+'index.php',
type: 'POST',
dataType: 'json',
data: data,
cache : false
}).done(function(response) {
alert(response);
});
return false; …Run Code Online (Sandbox Code Playgroud) 我开发了一个java代码,使用URL和HttpUrlConnection将以下cURL转换为java代码.卷曲是:
curl -i 'http://url.com' -X POST -H "Content-Type: application/json" -H "Accept: application/json" -d '{"auth": { "passwordCredentials": {"username": "adm", "password": "pwd"},"tenantName":"adm"}}'
Run Code Online (Sandbox Code Playgroud)
我编写了这段代码,但它始终给出了HTTP代码400错误的请求.我找不到遗漏的东西.
String url="http://url.com";
URL object=new URL(url);
HttpURLConnection con = (HttpURLConnection) object.openConnection();
con.setDoOutput(true);
con.setDoInput(true);
con.setRequestProperty("Content-Type", "application/json");
con.setRequestProperty("Accept", "application/json");
con.setRequestMethod("POST");
JSONObject cred = new JSONObject();
JSONObject auth = new JSONObject();
JSONObject parent = new JSONObject();
cred.put("username","adm");
cred.put("password", "pwd");
auth.put("tenantName", "adm");
auth.put("passwordCredentials", cred.toString());
parent.put("auth", auth.toString());
OutputStreamWriter wr = new OutputStreamWriter(con.getOutputStream());
wr.write(parent.toString());
wr.flush();
//display what returns the POST request
StringBuilder sb = new StringBuilder(); …Run Code Online (Sandbox Code Playgroud) 当我使用curlvia POST和set时CURLOPT_POSTFIELD,我必须urlencode或任何特殊格式?
例如:如果我想发布2个字段,第一个和最后一个:
first=John&last=Smith
Run Code Online (Sandbox Code Playgroud)
curl应该使用的确切代码/格式是什么?
$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_POSTFIELDS, $data);
$reply=curl_exec($ch);
curl_close($ch);
Run Code Online (Sandbox Code Playgroud) 我有一个PHP表单,其中包含已知数量的列(例如,顶部直径,底部直径,结构,颜色,数量),但行数未知,因为用户可以根据需要添加行.
我已经发现了如何获取每个字段(列)并将它们放入自己的数组中.
<input name="topdiameter['+current+']" type="text" id="topdiameter'+current+'" size="5" />
<input name="bottomdiameter['+current+']" type="text" id="bottomdiameter'+current+'" size="5" />
Run Code Online (Sandbox Code Playgroud)
所以我在HTML中最终得到的是:
<tr>
<td><input name="topdiameter[0]" type="text" id="topdiameter0" size="5" /></td>
<td><input name="bottomdiameter[0]" type="text" id="bottomdiameter0" size="5" /></td>
</tr>
<tr>
<td><input name="topdiameter[1]" type="text" id="topdiameter1" size="5" /></td>
<td><input name="bottomdiameter[1]" type="text" id="bottomdiameter1" size="5" /></td>
</tr>
...and so on.
Run Code Online (Sandbox Code Playgroud)
我现在要做的是将所有行和列放入多维数组中,并将其内容通过电子邮件发送给客户端(最好是格式很好的表).我无法真正理解如何将所有这些输入和选择组合成一个漂亮的数组.
在这一点上,我将不得不尝试使用几个1D阵列,尽管我认为使用单个2D阵列比使用多个1D阵列更好.
有没有办法使用javascript和JQuery添加一些使用POST从HTTP表单发送的其他字段?
我的意思是:
<form action="somewhere" method="POST" id="form">
<input type="submit" name="submit" value="Send" />
</form>
<script type="text/javascript">
$("#form").submit( function(eventObj) {
// I want to add a field "field" with value "value" here
// to the POST data
return true;
});
</script>
Run Code Online (Sandbox Code Playgroud) 我一直在寻找和阅读它,并没有任何真正有用的东西.
我正在编写一个小型C#win应用程序,允许用户将文件发送到Web服务器,而不是通过FTP,而是通过HTTP使用POST.可以把它想象成一个Web表单,但在Windows应用程序上运行.
我使用这样的东西创建了我的HttpWebRequest对象
HttpWebRequest req = WebRequest.Create(uri) as HttpWebRequest
Run Code Online (Sandbox Code Playgroud)
并且还设置了Method,ContentType和ContentLength属性.但那就是我能走的远.
这是我的一段代码:
HttpWebRequest req = WebRequest.Create(uri) as HttpWebRequest;
req.KeepAlive = false;
req.Method = "POST";
req.Credentials = new NetworkCredential(user.UserName, user.UserPassword);
req.PreAuthenticate = true;
req.ContentType = file.ContentType;
req.ContentLength = file.Length;
HttpWebResponse response = null;
try
{
response = req.GetResponse() as HttpWebResponse;
}
catch (Exception e)
{
}
Run Code Online (Sandbox Code Playgroud)
所以我的问题基本上是如何通过HTTP POST用C#发送文件(文本文件,图像,音频等).
谢谢!
我需要将JSON(我可以将其字符串化)发送到服务器并在用户端检索生成的JSON,而不使用JQuery.
如果我应该使用GET,我如何将JSON作为参数传递?是否存在太长的风险?
如果我应该使用POST,如何onload在GET中设置函数的等价物?
或者我应该使用不同的方法?
备注
这个问题不是关于发送一个简单的AJAX.它不应该作为重复关闭.