gpt4 book ai didi

swift - 无法使用 Swift 发出 Post 请求

转载 作者:行者123 更新时间:2023-11-30 10:15:44 25 4
gpt4 key购买 nike

我尝试用 swift 发出 post 请求,但它总是显示错误。我只是复制我的 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+PCFET0NUWVBFIEhUTUwgUFVCTElDICItLy9XM0MvL0RURCBIVE1MIDQuMCBUcmFuc2l0aW9uYWwvL0VOIj5kAgEPZBYMAgEPFgIeB1Zpc2libGVoZAICDxYCHgdjb250ZW50BQjXkdec15nXmmQCAw8WAh8CBQjXkdec15nXmmQCBA8WAh8CBSDXm9ecINeU15bXm9eV15nXldeqINep157Xldeo15XXqmQCBQ8WBB8CZB8BaGQCBg8WAh8CBQjXkdec15nXmmQCAg9kFgJmD2QWAgIED2QWAmYPZBYOAgEPZBYCAgEPZBYIAgEPDxYCHwFoZGQCAw8PFgIfAWhkZAIFD2QWAgICDxYCHwFoZAIHD2QWAgIBD2QWAgIBD2QWCmYPEGQQFQAVABQrAwBkZAIDDxYCHwFoZAIEDxYCHwFoZAIGD2QWAmYPZBYgZg9kFgICAQ8QZBAVPQbXmCAtIDEG15ggLSAyBteYIC0gMwbXmCAtIDQG15ggLSA1BteYIC0gNgbXmCAtIDcG15ggLSA4BteYIC0gOQfXmCAtIDEwB9eYIC0gMTEH15ggLSAxMgfXmCAtIDEzB9eYIC0gMTQH15ggLSAxNQbXmSAtIDEG15kgLSAyBteZIC0gMwbXmSAtIDQG15kgLSA1BteZIC0gNgbXmSAtIDcG15kgLSA4BteZIC0gOQfXmSAtIDEwB9eZIC0gMTEH15kgLSAxMgfXmSAtIDEzB9eZIC0gMTQH15kgLSAxNQfXmSAtIDE2CNeZ15AgLSAxCNeZ15AgLSA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PFgQfAwUKSGVhZGVyQ2VsbB8BaGQCCA8PFgIfAAU6157XoteV15PXm9efINecOiAwNi4wNS4yMDE1LCDXqdei15Q6IDIyOjQ5LCDXnteh15o6IEEzNzkxOWRkAgMPFgIfAwUZdG9wcmlnaHRwYW5lIEROTkVtcHR5UGFuZWQCBQ8WAh8DBRh0b3BsZWZ0cGFuZSBETk5FbXB0eVBhbmVkAgcPFgIfAwUWcmlnaHRwYW5lIEROTkVtcHR5UGFuZWQCCQ8WAh8DBRhjb250ZW50cGFuZSBETk5FbXB0eVBhbmVkAgsPFgIfAwUVbGVmdHBhbmUgRE5ORW1wdHlQYW5lZAINDxYCHwMFF2JvdHRvbXBhbmUgRE5ORW1wdHlQYW5lZGR/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";

当我在没有发出 post 请求的情况下读取文件时,它会显示正确的内容,但是当我发出 post 请求时,它会显示错误。

我该如何解决这个问题?我希望该网站向我显示我在应用程序中选择的右侧表格中的数据。 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+PCFET0NUWVBFIEhUTUwgUFVCTElDICItLy9XM0MvL0RURCBIVE1MIDQuMCBUcmFuc2l0aW9uYWwvL0VOIj5kAgEPZBYMAgEPFgIeB1Zpc2libGVoZAICDxYCHgdjb250ZW50BQjXkdec15nXmmQCAw8WAh8CBQjXkdec15nXmmQCBA8WAh8CBSDXm9ecINeU15bXm9eV15nXldeqINep157Xldeo15XXqmQCBQ8WBB8CZB8BaGQCBg8WAh8CBQjXkdec15nXmmQCAg9kFgJmD2QWAgIED2QWAmYPZBYOAgEPZBYCAgEPZBYIAgEPDxYCHwFoZGQCAw8PFgIfAWhkZAIFD2QWAgICDxYCHwFoZAIHD2QWAgIBD2QWAgIBD2QWCmYPEGQQFQAVABQrAwBkZAIDDxYCHwFoZAIEDxYCHwFoZAIGD2QWAmYPZBYgZg9kFgICAQ8QZBAVPQbXmCAtIDEG15ggLSAyBteYIC0gMwbXmCAtIDQG15ggLSA1BteYIC0gNgbXmCAtIDcG15ggLSA4BteYIC0gOQfXmCAtIDEwB9eYIC0gMTEH15ggLSAxMgfXmCAtIDEzB9eYIC0gMTQH15ggLSAxNQbXmSAtIDEG15kgLSAyBteZIC0gMwbXmSAtIDQG15kgLSA1BteZIC0gNgbXmSAtIDcG15kgLSA4BteZIC0gOQfXmSAtIDEwB9eZIC0gMTEH15kgLSAxMgfXmSAtIDEzB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8WBB8DBQpIZWFkZXJDZWxsHwFoZAITDxYCHwFoZAIUDxYEHwMFCkhlYWRlckNlbGwfAWhkAggPDxYCHwAFOtee16LXldeT15vXnyDXnDogMDYuMDUuMjAxNSwg16nXoteUOiAyMjo0OSwg157XodeaOiBBMzc5MTlkZAIDDxYCHwMFGXRvcHJpZ2h0cGFuZSBETk5FbXB0eVBhbmVkAgUPFgIfAwUYdG9wbGVmdHBhbmUgRE5ORW1wdHlQYW5lZAIHDxYCHwMFFnJpZ2h0cGFuZSBETk5FbXB0eVBhbmVkAgkPFgIfAwUYY29udGVudHBhbmUgRE5ORW1wdHlQYW5lZAILDxYCHwMFFWxlZnRwYW5lIEROTkVtcHR5UGFuZWQCDQ8WAh8DBRdib3R0b21wYW5lIEROTkVtcHR5UGFuZWRk6rOAIa+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)
})
}

最佳答案

尝试使用这个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)
})

这是最简单、最容易的方法。尝试一下,如果有任何问题,请告诉我。谢谢。

关于swift - 无法使用 Swift 发出 Post 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30094925/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com