The second day I can not cope with the site of the site https://smmok-fb.ru/ .
The fact is to get the API key I need to send an array with credentials (Mail, Password) in response as a JSON string I have to get the key. But in the end, it is impossible to send a request properly, please help with sending a post request.
URL API – http://smmok-fb.ru/api/getapikey
API documentation – https://smmok14.ru/api_docs
Class for desperilization JSON:
Public Class Requestsmmok
{
[JSONProperty ("Status")]
Public int Status {Get; SET; }
[JSONProperty ("Error")]
Public String Error {Get; SET; }
[Jsonproperty ("Respond")]
Public Respond [] Respond {Get; SET; }
}
Public Class Respond.
{
[JSONProperty ("API_KEY")]
Public String apikey {get; SET; }
}
My sample code:
using (httpclient client = new httpclient ())
{
client.default.parseaddd ("Mozilla / 4.0 (Compatible; MSIE 6.0; Windows NT 5.1; SV1)");
HttpContent Content = New FormurlencodedContent (New
{
New Keyvaluepair & lt; String, String & GT; ("Email", "[email protected]"),
New KeyValuepair & LT; String, String & GT; ("Password", "Value")
});
content.headers.contentType = New MediatypeHeaderValue ("Application / X-WWW-Form-Urlencoded");
Var Responsemessage =.
await client.postasync ("http://smmok-fb.ru/api/getapikey", content);
var left = jsonconvert.deserializeobject & lt; requestsmmok & gt; (responsemessage.content.readasstringasync (). result);
RETURN RESULT;
}
What I get in the end in the response – {“Status”: 400, “error”: “\ u041d \ u0435 \ u0443 \ u043a \ u0430 \ u043d email”} “
Answer 1, Authority 100%
- do not need to create
httpclient
for each request. iDisposable
classes need to be wrapped inusing
or dispensarly manually.FormurlencodedContent
automatically configure the content-type, you do not need to do this manually.result
In asynchronous code – this is an error, useAWAIT
Range on the example.
Announce HttpClient
separately, once at all time of the program.
private static readonly httpclient client = new httpclient ();
If you need a user-agent, add it too in a class designer, once. But I’m not sure that you need it at all, try first without him.
client.defaultRequestheaders.useragent.parseadd ("Mozilla / 4.0 Compatible; MSIE 6.0; Windows NT 5.1; sv1) ");
Well, the method itself.
private async task & lt; t & gt; Postapidata & lt; T & GT; (String URL, Dictionary & Lt; String, String & GT; Data)
{
Using (HttpContent CONTENT = New FormurlencodedContenttent (DATA))
Using (HttpResponsemessage Responsemessage = Await Client.postasync (URL, CONTENT) .configureawait (false))
{
String Jsontext = await responsemessage.content.readasstringasync (). Configureawait (False);
Return jsonconvert.deserializeObject & lt; T & GT; (JSontext);
}
}
Call
dictionary & lt; string, string & gt; PostData = New Dictionary & LT; String, String & GT; ();
postdata.add ("email", "[email protected]");
postdata.add ("Password", "Value");
Requestsmmok Result = Await Postapidata & lt; Requestsmmok & gt; ("https://smmok-fb.ru/api/getapikey", postdata);
Here is another error, you do not need an array, correct this way
[jsonproperty ("respond")]
Public Respond Respond {Get; SET; }
I will finish here:
The cause of the error was that the server wants HTTPS
request, and not http
, although this is not specified in the API documentation. Changed the link to https: //
, and everything worked.
Answer 2
If you translate UTF-8 characters into the string, the server responds: “Email not specified”. Try through Fiddler to see that in reality is sent to the server. Maybe he wants Email in another field, it’s hard to figure out here.