Hallo,
leider bin ich im Bezug auf Online-Anbindung der totale Anfänger und bekomme es leider nicht auf die Reihe. Ich habe eine ganz simple API, an die ich mich anbinden will. Daten von dort abholen via Webrequest habe ich hinbekommen.
Ich muß nun aber an diese Seite via Post wiederrum ein JSON zurück senden, finde hier aber kein Beispiel, das für mich funktioniert. Ich bekomme in folgendem Code dann z.B. "Der Remoteserver hat einen Fehler zurückgegeben: (422) Unprocessable Entity" zurück. Das Json ist aber das Beispiel des Anbieters und auch valide
Anhand der Fehlermeldung gehe ich eigentlich davon an, das die Verbindung zumind. soweit zustande kommt und ehr die Daten das Problem sind. Ist aber 1:1 das Beispiel aus der minimalistischen Doku.
Kann mir jemand verraten, ob der Code hier grundsätzlich wohl richtig ist bzw. ich hier ein unpassendes Beispiel angepasst habe?
leider bin ich im Bezug auf Online-Anbindung der totale Anfänger und bekomme es leider nicht auf die Reihe. Ich habe eine ganz simple API, an die ich mich anbinden will. Daten von dort abholen via Webrequest habe ich hinbekommen.
Ich muß nun aber an diese Seite via Post wiederrum ein JSON zurück senden, finde hier aber kein Beispiel, das für mich funktioniert. Ich bekomme in folgendem Code dann z.B. "Der Remoteserver hat einen Fehler zurückgegeben: (422) Unprocessable Entity" zurück. Das Json ist aber das Beispiel des Anbieters und auch valide
VB.NET-Quellcode
- Dim postData As String = "{'customer_order_reference':'70000001','comments_customer':'Please deliver asap','addressbook':{'country':'FR'},'order_products':[{'product_code':'274181','quantity':1,'addressbook':{'address':'Covent Garden','address2':'','city':'London','province':'','postal_code':'NR33 7NL','country':'GB','name':'Test Company','phone':'0684541247','comments':''}},{'product_code':'99999','quantity':1,'addressbook':{'address':'Covent Garden','address2':'','city':'London','province':'','postal_code':'NR33 7NL','country':'GB','name':'Test Company','phone':'0684541247','comments':''}}]}"
- Dim credentials As NetworkCredential = New NetworkCredential("rs@xxx.de", "12345")
- Dim encoding As New UTF8Encoding
- Dim byteData As Byte() = encoding.GetBytes(postData)
- Dim postReq As HttpWebRequest = DirectCast(WebRequest.Create("https://xxx.com/api_customer/orders"), HttpWebRequest)
- postReq.Method = "POST"
- postReq.KeepAlive = True
- postReq.Credentials = credentials
- postReq.ContentType = "application/x-www-form-urlencoded"
- postReq.Referer = ""
- postReq.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.1; ru; rv:1.9.2.3) Gecko/20100401 Firefox/4.0 (.NET CLR 3.5.30729)"
- postReq.ContentLength = byteData.Length
- Dim postreqstream As Stream = postReq.GetRequestStream()
- postreqstream.Write(byteData, 0, byteData.Length)
- postreqstream.Close()
- Dim postresponse As HttpWebResponse
- postresponse = DirectCast(postReq.GetResponse(), HttpWebResponse)
- Dim postreqreader As New StreamReader(postresponse.GetResponseStream())
- Dim thepage As String = postreqreader.ReadToEnd
- RichTextBox1.Text = thepage
Anhand der Fehlermeldung gehe ich eigentlich davon an, das die Verbindung zumind. soweit zustande kommt und ehr die Daten das Problem sind. Ist aber 1:1 das Beispiel aus der minimalistischen Doku.
Kann mir jemand verraten, ob der Code hier grundsätzlich wohl richtig ist bzw. ich hier ein unpassendes Beispiel angepasst habe?