1-) C# RMOS - HttpClient basit kullanımı utf8 destekli httpwebclient
mesaj.Replace("\r\n","\\n"); // gönderilen jsonda alt satıra geçmesi için bunu yaz
METODUN ÜSTÜNE BUNU YAZARSAN SWAGGER DE GÖZÜKMEZ
[ApiExplorerSettings(IgnoreApi = true)]
public string replaceTurkish(string text)
{
var json = @"{""name"":"""+ text + @"""}";
var name = JObject.Parse(json)["name"].ToString();
return name.ToString();
}
KULLANIMI
var name = replaceTurkish("Hills Young Sterilised Tavuklu K\u0131s\u0131rla\u015ft\u0131r\u0131lm\u0131\u015f Kedi Mamas\u0131 1.5 Kg")
TOKEN EKLEMEK
httpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token);
*********************** 1 (Api2.cs) ************************
KULLANIMI
Api2 api=new Api2();
private void button1_Click(object sender, EventArgs e)
{
string test = api.requestGet("https://www.gratis.com/flormar-brow-micro-filler-pen-kas-doldurucu-kalem/urun/Flormar47000097?sku=10181329");
}
PARAMETRESİZ POST GÖNDERME
public HttpResponseMessage mesajGonder(string tel, string mesaj) { try {
string url ="apiurl"; var request = new HttpRequestMessage(HttpMethod.Post, url); var response = client.SendAsync(request).Result;
return response; } catch (Exception ex) { throw ex; }
} |
*********************** 2 ************************
string json = JsonConvert.SerializeObject(model); var stringContent = new StringContent(json, Encoding.UTF8, "application/json"); GenelCevap results = JsonConvert.DeserializeObject<GenelCevap>(result);
// System.Net.ServicePointManager.SecurityProtocol |= SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls; bu sekilde header eklersin client.DefaultRequestHeaders.Add("token", token); client.DefaultRequestHeaders.Add("x-requested-with", "XMLHttpRequest");
JsonConvert.SerializeObject(model, Formatting.Indented);// formatlarsan güzel gözükür word wrap yapar
private void button1_Click(object sender, EventArgs e) { try { ServicePointManager.Expect100Continue = true; ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
Kullanici kullanici = new Kullanici(); kullanici.data.kullanici_ad = "ramazan"; kullanici.data.kullanici_mail = "mustafa@gmail.com"; kullanici.data.kullanici_sifre = "134"; kullanici.table = "kullanici"; kullanici.method = xenumGenel.insert;
var json = JsonConvert.SerializeObject(kullanici); var stringContent = new StringContent(json, Encoding.UTF8, "application/json");
CookieContainer cookies = new CookieContainer(); HttpClientHandler handler = new HttpClientHandler(); handler.CookieContainer = cookies; HttpClient client = new HttpClient(handler);
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("aplication/json")); HttpResponseMessage response = client.PostAsync(xenumGenel.url, stringContent).Result;
var byteArray = response.Content.ReadAsByteArrayAsync().Result; var result = Encoding.UTF8.GetString(byteArray, 0, byteArray.Length); GenelCevap results = JsonConvert.DeserializeObject<GenelCevap>(result); textBox1.Text = results.@return+"\n"+results.msg; } catch (Exception ex) { textBox1.Text = ex.Message; } }
2-) DİĞER
HttpClient client;
private void Form1_Load(object sender, EventArgs e) { ServicePointManager.Expect100Continue = true; ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; CookieContainer cookies = new CookieContainer(); HttpClientHandler handler = new HttpClientHandler(); handler.CookieContainer = cookies; client = new HttpClient(handler); }
public string requestPost(string url, Dictionary<string, string> dict) { client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("multipart/form-data")); var Content = new FormUrlEncodedContent(dict); HttpResponseMessage responseOtel = client.PostAsync(url, Content).Result; string result = responseOtel.Content.ReadAsStringAsync().Result; return result; }
public string requestPostJson(string url, object model) { string json = JsonConvert.SerializeObject(model); var stringContent = new StringContent(json, Encoding.UTF8, "application/json"); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); HttpResponseMessage responseOtel = client.PostAsync(url, stringContent).Result; string result = responseOtel.Content.ReadAsStringAsync().Result; return result; }
public string requestGet(string url) { client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("multipart/form-data")); HttpResponseMessage responseOtel = client.GetAsync(url).Result; string result = responseOtel.Content.ReadAsStringAsync().Result; result = System.Net.WebUtility.HtmlDecode(result); return result; }
public void loginOl() { var dict = new Dictionary<string, string>(); dict.Add("kullaniciAdi", "asd"); dict.Add("sifre", "asd"); string loginResponse = requestPost("https://ozerdemmotosiklet.com/Site/Giris", dict); }
2-) Resim Kaydetme
public void SaveImage(string filename, ImageFormat format, string imageUrl) { try { string name = Path.GetFileName(imageUrl); string[] allfiles = Directory.GetFiles(@"Resimler", "*.jpg", SearchOption.AllDirectories); // .txt veya .* foreach (var file in allfiles) { FileInfo info = new FileInfo(file); string fullname = info.Name.ToString(); if (name.Equals(fullname)) { return; } }
WebClient client = new WebClient(); Stream stream = client.OpenRead(imageUrl); Bitmap bitmap; bitmap = new Bitmap(stream); if (bitmap != null) { bitmap.Save(filename, format); } stream.Flush(); stream.Close(); client.Dispose(); } catch (Exception ex) {
}
}
** KULLANIMI **
SaveImage(resimYol, ImageFormat.Jpeg, "https://ozerdemmotosiklet.com/Resimler/" + item6.Resim1);
|