我尝试使用swift发布帖子请求,但它总是显示错误。我只是复制我的Chrome所做的帖子请求(通过开发人员工具)。
func postRequest(){
let request = NSMutableURLRequest(URL: NSURL(string: "http://blich.iscool.co.il/tabid/2117/language/he-IL/Default.aspx/")!)
request.HTTPMethod = "POST"
let postString = "__VIEWSTATE=/wEPDwUIMjU3MTQzOTcPZBYGZg8WAh4EVGV4dAU+PCFET0NUWVBFIEhUTUwgUFVCTElDICItLy9XM0MvL0RURCBIVE1MIDQuMCBUcmFuc2l0aW9uYWwvL0VOIj5kAgEPZBYMAgEPFgIeB1Zpc2libGVoZAICDxYCHgdjb250ZW50BQjXkdec15nXmmQCAw8WAh8CBQjXkdec15nXmmQCBA8WAh8CBSDXm9ecINeU15bXm9eV15nXldeqINep157Xldeo15XXqmQCBQ8WBB8CZB8BaGQCBg8WAh8CBQjXkdec15nXmmQCAg9kFgJmD2QWAgIED2QWAmYPZBYOAgEPZBYCAgEPZBYIAgEPDxYCHwFoZGQCAw8PFgIfAWhkZAIFD2QWAgICDxYCHwFoZAIHD2QWAgIBD2QWAgIBD2QWCmYPEGQQFQAVABQrAwBkZAIDDxYCHwFoZAIEDxYCHwFoZAIGD2QWAmYPZBYgZg9kFgICAQ8QZBAVPQbXmCAtIDEG15ggLSAyBteYIC0gMwbXmCAtIDQG15ggLSA1BteYIC0gNgbXmCAtIDcG15ggLSA4BteYIC0gOQfXmCAtIDEwB9eYIC0gMTEH15ggLSAxMgfXmCAtIDEzB9eYIC0gMTQH15ggLSAxNQbXmSAtIDEG15kgLSAyBteZIC0gMwbXmSAtIDQG15kgLSA1BteZIC0gNgbXmSAtIDcG15kgLSA4BteZIC0gOQfXmSAtIDEwB9eZIC0gMTEH15kgLSAxMgfXmSAtIDEzB9eZIC0gMTQH15kgLSAxNQfXmSAtIDE2CNeZ15AgLSAxCNeZ15AgLSAyCNeZ15AgLSAzCNeZ15AgLSA0CNeZ15AgLSA1CNeZ15AgLSA2CNeZ15AgLSA3CNeZ15AgLSA4CNeZ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15Q6IDIyOjQ5LCDXnteh15o6IEEzNzkxOWRkAgMPFgIfAwUZdG9wcmlnaHRwYW5lIEROTkVtcHR5UGFuZWQCBQ8WAh8DBRh0b3BsZWZ0cGFuZSBETk5FbXB0eVBhbmVkAgcPFgIfAwUWcmlnaHRwYW5lIEROTkVtcHR5UGFuZWQCCQ8WAh8DBRhjb250ZW50cGFuZSBETk5FbXB0eVBhbmVkAgsPFgIfAwUVbGVmdHBhbmUgRE5ORW1wdHlQYW5lZAINDxYCHwMFF2JvdHRvbXBhbmUgRE5ORW1wdHlQYW5lZGR/tCAa8PC5Gi4ObIdEaSbiOluCiA==&nn$ctr7919$TimeTableView$ClassesList=1&dnn$ctr7919$TimeTableView$MainControl$WeekShift=0&dnn$ctr7919$TimeTableView$ControlId=8"
request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding)
let task = NSURLSession.sharedSession().dataTaskWithRequest(request) {
data, response, error in
if error != nil {
println("error=\(error)")
return
}
println("response = \(response)")
let responseString = NSString(data: data, encoding: NSUTF8StringEncoding)
println("responseString = \(responseString)")
}
task.resume()
}
结果是:
response = <NSHTTPURLResponse: 0x7ffc90f054a0> { URL: http://blich.iscool.co.il/Default.aspx?tabid=2117&error=The+state+information+is+invalid+for+this+page+and+might+be+corrupted.&content=0 } { status code: 200, headers {
"Cache-Control" = private;
"Content-Encoding" = gzip;
"Content-Length" = 2875;
"Content-Type" = "text/html; charset=utf-8";
Date = "Thu, 07 May 2015 07:41:32 GMT";
Server = "Microsoft-IIS/7.5";
"Set-Cookie" = "DotNetNukeAnonymous=aaab9a64-e579-43f8-a83e-d4ed7c221f02; expires=Thu, 07-May-2015 08:01:32 GMT; path=/; HttpOnly, language=he-IL; path=/; HttpOnly";
Vary = "Accept-Encoding";
"X-AspNet-Version" = "2.0.50727";
"X-Powered-By" = "ASP.NET";
当我只是在没有发帖请求的情况下阅读文件时,它会向我显示正确的内容,但是当我发布帖子请求时就错了。
我该如何解决?我希望这个网站能够向我展示我在我的应用程序中选择的右表上的数据。 http://blich.iscool.co.il/tabid/2117/language/he-IL/Default.aspx/
这是当前代码:错误已修复 - 但仍然无效
func postRequest(){
SRWebClient.POST("http://blich.iscool.co.il/tabid/2117/language/he-IL/Default.aspx/").data(["__EVENTTARGET":"dnn$ctr7919$TimeTableView$btnChangesTable","__EVENTARGUMENT":"","__LASTFOCUS":"","__VIEWSTATE":"/wEPDwUIMjU3MTQzOTcPZBYGZg8WAh4EVGV4dAU+PCFET0NUWVBFIEhUTUwgUFVCTElDICItLy9XM0MvL0RURCBIVE1MIDQuMCBUcmFuc2l0aW9uYWwvL0VOIj5kAgEPZBYMAgEPFgIeB1Zpc2libGVoZAICDxYCHgdjb250ZW50BQjXkdec15nXmmQCAw8WAh8CBQjXkdec15nXmmQCBA8WAh8CBSDXm9ecINeU15bXm9eV15nXldeqINep157Xldeo15XXqmQCBQ8WBB8CZB8BaGQCBg8WAh8CBQjXkdec15nXmmQCAg9kFgJmD2QWAgIED2QWAmYPZBYOAgEPZBYCAgEPZBYIAgEPDxYCHwFoZGQCAw8PFgIfAWhkZAIFD2QWAgICDxYCHwFoZAIHD2QWAgIBD2QWAgIBD2QWCmYPEGQQFQAVABQrAwBkZAIDDxYCHwFoZAIEDxYCHwFoZAIGD2QWAmYPZBYgZg9kFgICAQ8QZBAVPQbXmCAtIDEG15ggLSAyBteYIC0gMwbXmCAtIDQG15ggLSA1BteYIC0gNgbXmCAtIDcG15ggLSA4BteYIC0gOQfXmCAtIDEwB9eYIC0gMTEH15ggLSAxMgfXmCAtIDEzB9eYIC0gMTQH15ggLSAxNQbXmSAtIDEG15kgLSAyBteZIC0gMwbXmSAtIDQG15kgLSA1BteZIC0gNgbXmSAtIDcG15kgLSA4BteZIC0gOQfXmSAtIDEwB9eZIC0gMTEH15kgLSAxMgfXmSAtIDEzB9eZIC0gMTQH15kgLSAxNQfXmSAtIDE2CNeZ15AgLSAxCNeZ15AgLSAyCNeZ15AgLSAzCNeZ15AgLSA0CNeZ15Ag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eT15vXnyDXnDogMDYuMDUuMjAxNSwg16nXoteUOiAyMjo0OSwg157XodeaOiBBMzc5MTlkZAIDDxYCHwMFGXRvcHJpZ2h0cGFuZSBETk5FbXB0eVBhbmVkAgUPFgIfAwUYdG9wbGVmdHBhbmUgRE5ORW1wdHlQYW5lZAIHDxYCHwMFFnJpZ2h0cGFuZSBETk5FbXB0eVBhbmVkAgkPFgIfAwUYY29udGVudHBhbmUgRE5ORW1wdHlQYW5lZAILDxYCHwMFFWxlZnRwYW5lIEROTkVtcHR5UGFuZWQCDQ8WAh8DBRdib3R0b21wYW5lIEROTkVtcHR5UGFuZWRk6rOAIa+r8tz1n6rsvSghvxeCHLo=","__VIEWSTATEGENERATOR":"CA0B0334","dnn$ctr7919$TimeTableView$ClassesList":"1","dnn$ctr7919$TimeTableView$ControlId":"","ScrollTop":"","__dnnVariable":""]).send({(response:AnyObject!, status:Int) -> Void in
//this is success part
println(response)
}, failure:{(error:NSError!) -> Void in
//this is failure part
println(error)
})
}
答案 0 :(得分:1)
尝试使用此SRWebClient。您只需将this swift file添加到项目中并将其另存为SRWebClient.swift
。
然后,当您发布时,您可以使用此代码。
SRWebClient.POST("http://blich.iscool.co.il/tabid/2117/language/he-IL/Default.aspx/")
.data(["yourFieldName":"yourValueForTheField"])
.send({(response:AnyObject!, status:Int) -> Void in
//this is success part
println(response)
}, failure:{(error:NSError!) -> Void in
//this is failure part
println(error)
})
这是一种简单易行的方法。尝试一下,如果有任何问题,请告诉我。感谢。
答案 1 :(得分:0)
我建议你使用图书馆&#34; Alamofire&#34;对于HTTP请求。这是链接:https://github.com/Alamofire/Alamofire
它让它变得如此简单:
request(.POST, url, parameters: parameters).responseJSON()
{
(request, response, data, error) in
if (data != nil)
{
// Do what you want
}
}
然后,随意使用SwiftyJSON解析JSON:https://github.com/SwiftyJSON/SwiftyJSON,或者你想做的任何事情。
编辑:对于参数,您需要创建一个这样的字典:
let parameter = [
"key1": "value1",
"key2": "value2"
]
但是所有内容都在&#34; Usage&#34;文档部分