Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
186 views
in Technique[技术] by (71.8m points)

iOS swift post реез request with binary body

I need to send an array of bytes to the server and receive another array of bytes in response, but the response comes


Status Code: 400, Headers {
     "Content-Type" = (
         "text / html; charset = UTF-8"
     );
}

my code

    let byteArray: [UInt8] = [UInt8](arrayLiteral: 0x05, 0x00, 0x07, 0x04, 0x00)
    let arrData = Data(byteArray)
    let requestURL = URL(string: "http://192.168.4.1/")!
    var request = URLRequest(url: requestURL)
    request.httpMethod = "POST"
    let session = URLSession(configuration: URLSessionConfiguration.default)
    request.setValue("application/octet-stream", forHTTPHeaderField: "Content-Type")
    request.addValue(String(arrData.count), forHTTPHeaderField: "Content-Length")
    request.httpBody = arrData
    var task = session.dataTask(with: request){ data, response, error in
        print("DATA (data ?? Data()) RESPONSE (response) ERROR (error)")
    }
    task.resume()

question from:https://stackoverflow.com/questions/65888948/ios-swift-post-%d1%80%d0%b5%d0%b5%d0%b7-request-with-binary-body

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Can't see why it does not work, but the first thing I would do is a simple local server that logs what it receives, and simulates what the device is supposed to send, to avoid mixing potential problems from everywhere.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...