ich entwickle mit dem .NET Framework 4.5 ein Baustein für ein Smart-Home-System. Genauer gesagt will ich über eine API zuerst eine Authentifizierung erreichen (klappt), dann Daten abrufen (klappt) und weitere Daten über einen weiteren Aufruf abrufen (klappt nicht).
Zuerst brauche ich POST, erhalte nach der Authentifizierung Cookies, die ich in einem CookieContainer speichere (klappt), diesen verwende ich dann wieder bei einem POST-Aufruf (klappt), dann benötige ich nochmals die Cookies für einen GET-Aufruf (klappt nicht). Da erhalte ich dann die Fehlermeldung "Inhaltsteil mit diesem Verbtyp kann nicht gesendet werden.".
Ich habe über Google herausgefunden, dass die Cookies nicht so einfach über GET übermittelt werden können?
Ich würde mich sehr über eine Idee freuen!
Alex
NUR AUSSCHNITT:
public const string userAgent = "Mozilla/5.0 (Windows NT 6.1; rv:7.0.1) Gecko/20100101 Firefox/7.0.1";
public CookieContainer cookieJar;
private CookieContainer Login(string username, string password, string loginPageURL)
{
// Create a request using the provied URL.
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(loginPageURL);
// Set the Method property of the request to POST.
request.Method = "POST";
// Set the Cookiecontainer
CookieContainer cookieJar = new CookieContainer();
request.CookieContainer = cookieJar;
// Create POST data and convert it to a byte array.
//string postData = "username=" + username + "&password=" + password;
//byte[] byteArray = Encoding.UTF8.GetBytes(postData);
// Set the ContentType property of the WebRequest.
request.ContentType = "application/x-www-form-urlencoded";
// Set the User Agent
request.UserAgent = userAgent;
// Set the ContentLength property of the WebRequest.
//request.ContentLength = byteArray.Length;
// Get the request stream.
Stream dataStream = request.GetRequestStream();
// Write the data to the request stream.
//dataStream.Write(byteArray, 0, byteArray.Length);
// Close the Stream object.
dataStream.Close();
// Get the response.
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
// Get the stream containing content returned by the server.
dataStream = response.GetResponseStream();
// Open the stream using a StreamReader for easy access.
StreamReader reader = new StreamReader(dataStream);
// Read the content.
string responseFromServer = reader.ReadToEnd();
string cookie = cookieJar.GetCookieHeader(request.RequestUri);
Login ID = JsonConvert.DeserializeObject<Login>(responseFromServer);
this.PlantID.Value = ID.Back.Data[0].PlantId;
// Clean up the streams.
reader.Close();
dataStream.Close();
response.Close();
return cookieJar;
}
private string ViewPageGET(CookieContainer cookieJar, string pageURL)
{
string str = string.Empty;
HttpWebRequest request2 = (HttpWebRequest)WebRequest.Create(pageURL);
// Set the Method property of the request to GET.
request2.Method = "GET";
// Put session back into CookieContainer
request2.CookieContainer = cookieJar;
// Get the request stream.
Stream dataStream2 = request2.GetRequestStream();
// Close the Stream object.
dataStream2.Close();
// Get the response.
HttpWebResponse response2 = (HttpWebResponse)request2.GetResponse();
// Get the stream containing content returned by the server.
dataStream2 = response2.GetResponseStream();
// Open the stream using a StreamReader for easy access.
StreamReader reader2 = new StreamReader(dataStream2);
// Read the content.
str = reader2.ReadToEnd();
return str;
}
public override void Execute()
{
if (this.Trigger.WasSet && this.Trigger.HasValue)
{
try
{
// URL für Login
string loginPageURL = "https://server.growatt.com/newTwoLoginAPI.do?userName=" + this.Benutzername + "&password=" + MD5Passwort;
// URL für Jahresproduktion - GET
string pageURL2 = "https://server.growatt.com/newPlantDetailAPI.do?plantId=" + this.PlantID + "&date=" + DateTime.Now.Year.ToString() + "&type=3";
// URL für Tagesproduktion - GET
string pageURL3 = "https://server.growatt.com/newPlantDetailAPI.do?plantId=" + this.PlantID + "&date=" + DateTime.Now.Date.ToString("yyyy-mm-dd") + "&type=1";
// Login-Aufruf
cookieJar = Login(this.Benutzername, this.Passwort, loginPageURL);
// Aufruf 2. API (GET)
Root2 Growattlogin2 = JsonConvert.DeserializeObject<Root2>(ViewPageGET(cookieJar, pageURL2));
// Aufruf 3. API (GET)
Root2 Growattlogin3 = JsonConvert.DeserializeObject<Root2>(ViewPageGET(cookieJar, pageURL3));
this.Year.Value = (float) Convert.ToDouble(Growattlogin2.Back.PlantData.CurrentEnergy.Replace(" kWh", ""));
this.Today.Value = (float) Convert.ToDouble(Growattlogin3.Back.PlantData.CurrentEnergy.Replace(" kWh", ""));
this.Fehler.Value = string.Empty;
}
catch (Exception ex)
{
this.Fehler.Value = ex.Message;
}
}
}