Ich möchte das in der Funktion empfangene Objekt konvertieren und tun, um es in ein Objekt ({"some_key": "some_value"})
zu konvertieren.HTTP-Anfrage mit C#
Hier ist mein Code:
public HttpRequests(string url, string method, Object data)
{
//The following prepares data, according to received parameter
if (data is Array)
{
data = (Array)data;
}
else if (data is Dictionary<Object, Object>)
{
data = ((Dictionary<string, string>)data)["something"] = platform_secret;
data = ((Dictionary<string, string>)data)["something2"] = "1";
}
method = method.ToUpper(); //POST or GET
this.url = just_url + url;
this.data = Newtonsoft.Json.JsonConvert.SerializeObject(data);
this.method = method;
}
public Object performRequest()
{
if (this.data != null && url != null)
{
WebRequest request = HttpWebRequest.Create(url);
byte[] data_bytes = Encoding.ASCII.GetBytes(Convert.ToChar(data)[]);
//^ this does not work. Am I supposed to do this?
// as I said, what I want is to get an object {key: something} that can be read
// by $_POST["key"] in the server
request.Method = method;
request.ContentType = "application/x-www-form-urlencoded"; //TODO: check
//request.ContentLength = ((Dictionary<string, string>) data);
request.ContentLength = data_bytes.Length;
Stream dataStream = request.GetRequestStream(); //TODO: not async at the moment
//{BEGIN DOUBT
dataStream.Write(data_bytes, 0, data_bytes.Length);
dataStream.Close();
//DOUBT: DO THIS^or THIS:_ ???
StreamWriter writer = new StreamWriter(dataStream);
writer.Write(this.data);
//End DOUBT}
WebResponse response = request.GetResponse();
Stream dataResponse = response.GetResponseStream();
writer.Close();
response.Close();
dataStream.Close();
return dataResponse.
}
Was genau bin ich hier fehlt?
Hast du versucht zu debuggen, was in 'this.data' steckt? Auch verwenden Sie 'Daten' anstelle von' this.data' – Clijsters
Ihr Code ist falsch oder unvollständig. Die obere Funktion hat keinen Rückgabetyp oder Funktionsnamen, und die Variablen platform_secret und just_url sind nicht definiert. Kannst du Arbeitscode liefern? – martijn
Bitte fügen Sie eine klare Erklärung Ihres Problems oder Ihrer Frage hinzu. "Was ich vermisse" ist keine echte Frage. – cassandrad