How to pass data.bind in POST request using C# - c#

i have a web app connected with crm Dynamics 365. I want to create record in an entity using API, but the entity has lookup fields to another entities. I tried this code but it gives bad request error. How can i send the values of the lookup fields in the API?
JSON:{
"msdyn_name": "Asset",
"msdyn_account#odata.bind": "/accounts(7f7e031b-0e20-ea11-a810-000d3a2d54fd)",
"new_externalproj#odata.bind": "/msdynce_externalprojects(ee3f03c6-751f-ea11-a810-000d3a27b751)" }
var serializer = new JavaScriptSerializer();
string json = serializer.Serialize(new
{
msdyn_name = "Asset",
msdyn_account = "7f7e031b-0e20-ea11-a810-000d3a2d54fd",
new_externalproj = "ee3f03c6-751f-ea11-a810-000d3a27b751"
});
var httpWebRequest = (HttpWebRequest)WebRequest.Create("https://XXX.dynamics.com/api/data/v9.1/msdyn_customerassets");
httpWebRequest.Method = "POST";
httpWebRequest.Headers.Add("Authorization", String.Format("Bearer {0}", result.AccessToken));
httpWebRequest.Headers.Add("OData-MaxVersion", "4.0");
httpWebRequest.Headers.Add("OData-Version", "4.0");
httpWebRequest.ContentType = "application/json";
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{streamWriter.Write(json);
}
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
var responseText = streamReader.ReadToEnd();
}

Related

delete a Firebase user using C#

Is it possible to delete a user account in Firebase in C#?
I can successfully create users but on the delete, I get INSUFFICIENT_PERMISSION bu I don't know how to provide the correct permissions.
The code for creating a user:
var httpWebRequest = (HttpWebRequest)WebRequest.Create(NEW_USER_URL);
httpWebRequest.ContentType = "application/json";
httpWebRequest.Method = "POST";
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
var body = new
{
email = email,
password = password
};
string json = JObject.FromObject(body).ToString();
streamWriter.Write(json);
streamWriter.Flush();
streamWriter.Close();
}
The code for deleting a user:
var httpWebRequest = (HttpWebRequest)WebRequest.Create(DELETE_USER_URL);
httpWebRequest.ContentType = "application/json";
httpWebRequest.Method = "POST";
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
var body = new
{
localId = uUid
};
string json = JObject.FromObject(body).ToString();
streamWriter.Write(json);
streamWriter.Flush();
streamWriter.Close();
}
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
The urls:
static string NEW_USER_URL = $#"https://www.googleapis.com/identitytoolkit/v3/relyingparty/signupNewUser?key={ConfigurationManager.AppSettings["FireBaseProjectApiKey"]}";
static string DELETE_USER_URL = $#"https://www.googleapis.com/identitytoolkit/v3/relyingparty/deleteAccount?key={ConfigurationManager.AppSettings["FireBaseProjectApiKey"]}";

How can i post json with cookies to server with c# webrequest?

I'm new in c# ,and want to send json post to server with this code:
var httpWebReques = (HttpWebRequest) WebRequest.Create(#"http://myDomain/api/FileCommand/AddGig ");
httpWebReques.ContentType = "application/json";
httpWebReques.Method = "POST";
using (var streamWriter=new StreamWriter(httpWebReques.GetRequestStream()))
{
JObject obj = JObject.FromObject(new
{
PhoneNumber= phoneNumber,
TrafficGigCount="1",
PaymentAmountRial="2000",
PaymentTrackingNo="123434"
});
string json = obj.ToString(); //"{\"PhoneNumber\":\""+phoneNumber+"\"," + "\"TrafficGigCount\":\"1\","+ "\"PaymentAmountRial\":\"1000\","+ "\"PaymentTrackingNo\":\"1254\"}";
streamWriter.Write(obj);
streamWriter.Flush();
streamWriter.Close();
}
var httpResponse = (HttpWebResponse) httpWebReques.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
var result = streamReader.ReadToEnd();
}
every thing is ok and now want to send server cookie with this code:
httpWebReques.CookieContainer = new CookieContainer();
httpWebReques.CookieContainer.SetCookies(new Uri("http://www.tclnet.ir/"), cookies);
for that purpose write this code:<br/>
var httpWebReques = (HttpWebRequest) WebRequest.Create(#"http://www.tclnet.ir/api/FileCommand/AddGig ");
httpWebReques.ContentType = "application/json";
httpWebReques.Method = "POST";
httpWebReques.CookieContainer = new CookieContainer();
httpWebReques.CookieContainer.SetCookies(new Uri("http://www.tclnet.ir/"), cookies);
using (var streamWriter=new StreamWriter(httpWebReques.GetRequestStream()))
...
but when receive to this line:
var httpResponse = (HttpWebResponse) httpWebReques.GetResponse();
get this error:
The remote server returned an error: (500) Internal Server Error.
How can i solve that?thanks.
my error:

How do I send a json array to an ASP.NET server?

I wrote this code to send some JSON to a server:
var httpWebRequest = (HttpWebRequest)WebRequest.Create("http://www.brilliantit.ir/getDetail.aspx");
httpWebRequest.ContentType = "text/json";
httpWebRequest.Method = "POST";
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
//string json = "{\"id\":\"ahbarres\"}";
string json = "{\"id\":\""+label1.Text+"\"}";
streamWriter.Write(json);
streamWriter.Flush();
streamWriter.Close();
}
String temp = "";
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
var result = streamReader.ReadToEnd();
temp = result.ToString();
}
But, I want to send a JSON array to a server. How can I do this?

PHP service to C#

in php i post a request like
$.post("/service.php?cat=c1", {
group: $this.attr('href'),
})
where group -> #$!/mycat/year,2012
now i want to do same request in c#
var httpWebRequest = (HttpWebRequest)WebRequest.Create( url);
httpWebRequest.ContentType = "text/json";
httpWebRequest.Method = "POST";
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
result = streamReader.ReadToEnd();
}
I tried
String url = domain + "./service.php?cat=c1&group=%22#$!/mycat/year,2012%22";
and
String url = domain + "./service.php?cat=c1&group=mycat&year=2012";
but "message" always returns empty
What is the problem here?
You can use the WebClient class, as this is easier to use.
Pass in the values as NameValueCollection object
var client = new WebClient();
var nameValueCollection = HttpUtility.ParseQueryString("cat=c1&group=mycat&year=2012");
var response = client.UploadValues(domain + "/service.php","POST",nameValueCollection);
var responseStr = Encoding.ASCII.GetString(response);

HttpWebRequest with proxy - setting cookies

Here is my code:
var httpWebRequest = (HttpWebRequest)WebRequest.Create("http://localhost/jsonrpc.cgi");
httpWebRequest.ContentType = "application/json";
httpWebRequest.Method = "POST";
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
string json = "someParameters";
streamWriter.Write(json);
}
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
var responseText = streamReader.ReadToEnd();
}
string Bugzilla_logincookie= httpResponse.Headers.ToString();
Bugzilla_logincookie= Bugzilla_logincookie.Substring(plsWork .IndexOf("logincookie") + 12);
Bugzilla_logincookie= Bugzilla_logincookie.Substring(0, plsWork .IndexOf(";"));
CookieContainer cc = new CookieContainer();
cc.SetCookies(new Uri("http://localhost"), Bugzilla_logincookie);
var httpWebRequest2 = (HttpWebRequest)WebRequest.Create("http://localhost/jsonrpc.cgi");
httpWebRequest2.ContentType = "application/json";
httpWebRequest2.Method = "POST";
httpWebRequest2.Proxy.Credentials = new NetworkCredential("username", "password");
httpWebRequest2.CookieContainer = cc;
using (var streamWriter2 = new StreamWriter(httpWebRequest2.GetRequestStream()))
{
string json = "someParametersForJsonCall";
streamWriter2.Write(json);
}
var httpResponse2 = (HttpWebResponse)httpWebRequest2.GetResponse();
using (var streamReader2 = new StreamReader(httpResponse2.GetResponseStream()))
{
var responseText = streamReader2.ReadToEnd();
}
I have problem with using proxy. The thing I'm trying to do is: use a Proxy for http://www.bugzilla.org/docs/tip/en/html/api/Bugzilla/WebService/User.html to call the login method and then store cookies of response and send them with each call of the session.
I get this error:
"You must log in before using this part of Bugzilla."
What am I mistakenly using?

Categories

Resources