I'm getting a json from httpWebResponse which supposed to be in the following format :
{"d":"{\"Success\":\"\",\"PricedItineraries\":[{\"IsRecommendedFlight\":false,\"BookingClass\":null,\"AirItinerary\":{\"OriginDestinationOptions\":{\"OriginDestinationOption\":[{\"FlightSegment\":[{\"DepartureAirport\":{\"LocationCode\":\"RAK\",\"Terminal\":\"1\"},\"ArrivalAirport\":{\"LocationCode\":\"ORY\",\"Terminal\":\"S\"},\"Equipment\":{\"AirEquipType\":\"73H\",\"ChangeofGauge\":\"true\"},\"MarketingAirline\":{\"Code\":\"TO\",\"CodeContext\":\"KBATO\"},\"OperatingAirline\":{\"Code\":\"TO\"},\"BookingClassAvails\":[{\"ResBookDesigCode\":\"A\",\"RPH\":\"5\"}],\"BagAllownceInfo\":{\"Allowance\":\"00\",\"QuantityCode\":\" N\",\"UnitQualifier\":\" K\"},\"FareID\":\"000000000\",\"Token\":\"00000000-0000-0000-0000-000000000000\",\"AdultBaseFare\":\"0000517.77\",\"AdultTaxFare\":\"0000000.00\",\"ChildBaseFare\":\"0000000.00\",\"ChildTaxFare\":\"0000000.00\",\"InfantBaseFare\":\"0000000.00\",\"InfantTaxFare\":\"0000000.00\",\"PriceTotal\":\"0000307.90\",\"LFID\":\"0000000\",\"PFID\":\"00000\",\"PTFID\":\"
I tried to Deserialize the json using JSonConverter but it returns stream was not readable:
public static object DeserializeFromStream(Stream stream)
{
var serializer = new JsonSerializer();
using (var sr = new StreamReader(stream))
using (var jsonTextReader = new JsonTextReader(sr))
{
return serializer.Deserialize(jsonTextReader);
}
}
So I'm reading my httpwebresponse:
using (StreamReader Reader = new StreamReader(ResStream))
{
this.ResponseHTML = Reader.ReadToEnd();
}
But somehow it returns:
?W?S???"??yE,?????♣*Ay◄?lA+??F◄K?\?b;^~§????xN?yU,6U,?☺♣U,T??O?♠V►R?B§??*????↨??
e?-|T?P?b???s§???M§♂U,?'??*?^le?????▬????§%7↕???f??Qd←|?c♣??
bq7;???ffv%)?▬??↔L6???s?V?~?#?$♀]☺◄????D????'X?e?_?????"??E??Q]E,Ad-? ♥?Xb'??K
[???y;?d"0??:?-X??←Xòs←▬?→?$?↑-b☼}E,???"??MF??j↨??;vb)?aq?ai???R?.5,???????→▬jX?
♂5,?↑j??P?2?→?????+5,?tu?#??ev??☺7;???o☺??3w???P?B
w???E?4!?→??MF??j??v?→▬?→???]jX?I?R???⌂?i]?5,a↕☼↓????→▬???kX??mjX?a◄1?♥'P?;??(??
♂??????H?aq??→▬??↑e???[§???%M?.^??}X???z??t??an?→▬c????_~Z?→▬jX?a?gZ?→▬jX?a?????
♂5,?↑j??P?2?→▬?a)Y??a????>,
5,??a?P?Ro▼▬?→???]jX?I????%t-jX8K?→▬jXLI?a?,!jX??%??PO??P?R?f???↔/4?f?c?7;?jX6?L
I'm sending the webrequest :
public Response Send()
{
if (REQUEST == null)
return new Response(REQUEST) { HTTPStatusCode = "999" };
WebResponse Res = null;
CookieContainer Cookies = new CookieContainer();
if (REQUEST.Cookies != null)
{
Cookies = REQUEST.Cookies;
}
bool isdone = true;
DateTime Time = default(DateTime);
Sender = (HttpWebRequest)WebRequest.Create(this.REQUEST._url);
Sender.Host = REQUEST.Host;
Sender.Accept = REQUEST.Accept;
Sender.Method = REQUEST.Method;
Sender.UserAgent = REQUEST.UserAgent;
Sender.ContentType = REQUEST.ContentType;
Sender.CookieContainer = Cookies;
Sender.Referer = REQUEST.Refer;
if (REQUEST.Data != null && REQUEST.Data.Length > 0)
{
using (var writer = new StreamWriter(Sender.GetRequestStream()))
{
writer.Write(REQUEST.Data);
writer.Flush();
writer.Close();
}
}
try
{
Res = Sender.GetResponse();
Time = DateTime.Now;
}
catch (Exception Ex)
{
isdone = false;
if (OnExceptionHappened != null)
OnExceptionHappened(this, new ExceptionArgs { Msg = Ex.Message, Sender = this, Time = DateTime.Now });
}
return AssignWebResponse((HttpWebResponse)Res, isdone, Time, Cookies, Sender.RequestUri.AbsoluteUri);
}
Request :
var REQUEST = new Request()
{
_url = "https://www.example.com/",
Host = "host",
Method = "POST",
Refer = "refer",
UserAgent = "Mozilla/5.0 (Windows NT 6.3; Win64; x64; rv:56.0) Gecko/20100101 Firefox/56.0",
ContentType = "application/json",
Cookies = CurrentContainer,
Data = "{ 'isFromCache': 'undefined', 'pageNumber': '1'}",
isJson = true
};
using (BeRequest.CORE.BeRequest Req = new BeRequest.CORE.BeRequest(REQUEST))
{
Req.OnExceptionHappened += Req_OnExceptionHappened;
var response = Req.Send();
return response.ResponseHTML;
}
Just do:
using (StreamReader Reader = new StreamReader(Res.GetResponseStream()))
{
var myObject = JsonConvert.DeserializeObject<YourClass>(Reader.ReadToEnd());
}
Your code is a bit all over the place. But assuming that Res is of type WebResponse, then it should work.
Related
I need to make an rpc to a thirh party API and send the following JSON
{
"jsonrpc":"2.0",
"id":"number",
"method":"login.user",
"params":{
"login":"string",
"password":"string"
}
}
I have created a method to make the rcp but i cannot get the correct JSON to be send
public JObject Post()
{
object[] a_params = new object[] { "\"login\" : \"test#test.ru\"", "\"password\": \"Password\""};
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create("https://test.test.ru/v2.0");
webRequest.ContentType = "application/json; charset=UTF-8";
webRequest.Method = "POST";
JObject joe = new JObject();
joe["jsonrpc"] = "2.0";
joe["id"] = 1;
joe["method"] = "login.user";
if (a_params != null)
{
if (a_params.Length > 0)
{
JArray props = new JArray();
foreach (var p in a_params)
{
props.Add(p);
}
joe.Add(new JProperty("params", props));
}
}
string s = JsonConvert.SerializeObject(joe);
// serialize json for the request
byte[] byteArray = Encoding.UTF8.GetBytes(s);
webRequest.ContentLength = byteArray.Length;
WebResponse webResponse = null;
try
{
using (webResponse = webRequest.GetResponse())
{
using (Stream str = webResponse.GetResponseStream())
{
using (StreamReader sr = new StreamReader(str))
{
return JsonConvert.DeserializeObject<JObject>(sr.ReadToEnd());
}
}
}
}
catch (WebException webex)
{
using (Stream str = webex.Response.GetResponseStream())
{
using (StreamReader sr = new StreamReader(str))
{
var tempRet = JsonConvert.DeserializeObject<JObject>(sr.ReadToEnd());
return tempRet;
}
}
}
catch (Exception)
{
throw;
}
}
With my code i'm getting the following JSON
{"jsonrpc":"2.0","id":1,"method":"login.user","params":["\"login\" : \"v.ermachenkov#mangazeya.ru\"","\"password\": \"AmaYABzP2\""]}
As i understand my error is that params is an array([]) instead of an object({}). Based on my method how can get the correct json?
I correct my mistake. The code shoukd be
JObject a_params = new JObject { new JProperty("login", "login"), new JProperty("password", "Password") };
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create("https://test.test.ru/v2.0");
webRequest.ContentType = "application/json; charset=UTF-8";
webRequest.Method = "POST";
JObject joe = new JObject();
joe["jsonrpc"] = "2.0";
joe["id"] = "1";
joe["method"] = "login.user";
joe.Add(new JProperty("params", a_params));
I have written a complex class called DonationListener. This class has a property named onDonation that takes a function that will be called when a donation happens. In a simple console program it works fine, but in the more complex WinForms application - not. I am pretty sure that there is no mistake in the class. So I think the problem is in the usage of it. My class works with WebSockets, so I decided to check with the fiddler if the connection lost after the first donation event, and the answer is no, the connection is fine and sending packages that should invoke onDonation. Here's the code
public string donation_password;
DonationListener dl;
public System.Windows.Forms.PictureBox[,] eliteCards;
public mainForm()
{
InitializeComponent();
initializeDonation();
}
public void initializeDonation()
{
donation_password = db.getPassword();
dl = new DonationListener(donation_password);
dl.OnDonation = donation =>
{
overlay ol = new overlay();
string username = donation["username"].ToString();
User toRide = db.getUserByField(username);
ol.Show();
ol.ride(toRide);
};
Task t = new Task(() => {
dl.DoListen();
});
t.Start();
}
Full class DonationListener(if u need sth from it)
using System;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.WebSockets;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace RacheM
{
public class DonationListener
{
private string addUserTemplate = "69:42[\"add-user\",{{\"token\":\"{0}\",\"type\":\"alert_widget\"}}]";
public Action<JObject> OnDonation = null;
private readonly string _token;
public DonationListener(string token)
{
_token = token;
}
public void DoListen()
{
var result =
DoRequest("GET", "https://socket.donationalerts.ru/socket.io/?EIO=3&transport=polling"); //&t=MYotE0N
var sid = ExtractToken(result);
if (string.IsNullOrEmpty(sid))
{
throw new Exception("Failed to get sid");
}
var cookie = new Cookie("io", sid, "/", "socket.donationalerts.ru");
DoRequest(
"POST",
$"https://socket.donationalerts.ru/socket.io/?EIO=3&transport=polling&sid={sid}",
string.Format(addUserTemplate, _token),
cookie
);
var waiter = DoWebSocket($"wss://socket.donationalerts.ru/socket.io/?EIO=3&transport=websocket&sid={sid}",
cookie);
waiter.GetAwaiter().GetResult();
}
private static string ExtractToken(string strWidthToken)
{
var m = Regex.Match(strWidthToken, "\"sid\":\"(?<token>[^\"]+)\"");
return m.Groups["token"].Value;
}
private static string DoRequest(string method, string url, string data = "", Cookie cookie = null)
{
var httpWebRequest = (HttpWebRequest) WebRequest.Create(url);
httpWebRequest.Method = method;
httpWebRequest.Accept = "*/*";
httpWebRequest.Headers.Add("Origin", "https://www.donationalerts.com");
httpWebRequest.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip, deflate, br");
httpWebRequest.Headers.Add(HttpRequestHeader.AcceptLanguage, "en-US,en;q=0.9");
httpWebRequest.UserAgent = "Mozilla/5.0 (Windows NT 6.2; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.84 Safari/537.36";
httpWebRequest.Referer = "https://www.donationalerts.com/widget/alerts?group_id=1&token=mcq71m8KVIsojvo5ukFZ";
if (method == "GET")
{
//httpWebRequest.Headers.Remove("Content-Length");
httpWebRequest.ContentLength = 0;
}
else
{
httpWebRequest.ContentType = "text/plain;charset=UTF-8";
}
if (cookie != null)
{
if (httpWebRequest.CookieContainer == null)
{
httpWebRequest.CookieContainer = new CookieContainer();
}
httpWebRequest.CookieContainer.Add(cookie);
}
if (method == "POST" && !string.IsNullOrEmpty(data))
{
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
streamWriter.Write(data);
streamWriter.Flush();
streamWriter.Close();
}
}
var httpResponse = (HttpWebResponse) httpWebRequest.GetResponse();
var streamReader = new StreamReader(httpResponse.GetResponseStream());
if(streamReader == null)
{
throw new Exception("Connection error");
}
using (streamReader)
{
return streamReader.ReadToEnd();
}
}
private async Task DoWebSocket(string url, Cookie cookie)
{
using (var ws = new ClientWebSocket())
{
var serverUri = new Uri(url);
ws.Options.Cookies = new CookieContainer();
ws.Options.Cookies.Add(cookie);
await ws.ConnectAsync(serverUri, CancellationToken.None);
var cts = new CancellationTokenSource();
Task.Factory.StartNew(
async () =>
{
while (true)
{
try
{
string srcMessage = string.Empty;
ArraySegment<Byte> buffer = new ArraySegment<byte>(new Byte[8192]);
using (var ms = new MemoryStream())
{
WebSocketReceiveResult result= null;
do
{
result = await ws.ReceiveAsync(buffer, CancellationToken.None);
ms.Write(buffer.Array, buffer.Offset, result.Count);
} while (!result.EndOfMessage);
ms.Seek(0, SeekOrigin.Begin);
if (result.MessageType == WebSocketMessageType.Text)
{
using (var reader = new StreamReader(ms, Encoding.UTF8))
{
srcMessage = reader.ReadToEnd();
}
}
}
if (string.IsNullOrEmpty(srcMessage)) continue;
if (srcMessage.IndexOf("42[\"donation", StringComparison.Ordinal) == 0)
{
OnDonation?.Invoke(ParseDonateMessage(srcMessage));
}
else
{
Console.WriteLine("no donate msg: " + srcMessage);
}
}
catch (Exception ex)
{
Console.WriteLine("Error: " + ex.Message);
}
}
}, cts.Token, TaskCreationOptions.LongRunning, TaskScheduler.Default);
await SendWsMessage(ws, "2probe");
await SendWsMessage(ws, "5");
while (ws.State == WebSocketState.Open)
{
await SendWsMessage(ws, "2");
Thread.Sleep(25000);
}
}
}
private static async Task SendWsMessage(WebSocket ws, string message)
{
var sendBytes = Encoding.UTF8.GetBytes(message);
var cts = new CancellationTokenSource();
var sendBuffer = new ArraySegment<byte>(sendBytes);
await
ws.SendAsync(sendBuffer, WebSocketMessageType.Text, endOfMessage: true,
cancellationToken: cts.Token);
}
private static JObject ParseDonateMessage(string rcvMsg)
{
rcvMsg = rcvMsg.Replace("42[\"donation\",\"", "");
rcvMsg = rcvMsg.Substring(0, rcvMsg.Length - 2);
return (JObject)JsonConvert.DeserializeObject(Regex.Unescape(rcvMsg));
}
}
}
Maybe you should try to make this function repeat itself by:
bool RunFlag = true // can be set to false somewhere else
public void DoListen()
{
while(RunFlag) {
var result =
DoRequest("GET", "https://socket.donationalerts.ru/socket.io/?EIO=3&transport=polling"); //&t=MYotE0N
var sid = ExtractToken(result);
if (string.IsNullOrEmpty(sid))
{
throw new Exception("Failed to get sid");
}
var cookie = new Cookie("io", sid, "/", "socket.donationalerts.ru");
DoRequest(
"POST",
$"https://socket.donationalerts.ru/socket.io/?EIO=3&transport=polling&sid={sid}",
string.Format(addUserTemplate, _token),
cookie
);
var waiter = DoWebSocket($"wss://socket.donationalerts.ru/socket.io/?EIO=3&transport=websocket&sid={sid}",
cookie);
waiter.GetAwaiter().GetResult();
}
}
Hi I'm trying to login via https://www.strava.com/session with HttpWebrequest but it doesn't log me in. It gives me an response of 302 which is good but it never redirect me to https://www.strava.com/dashboard.
this is the code that I'm using
Httpclient:
public class HttpClient
{
private const string UserAgent = "Mozilla/5.0";
public CookieCollection CookieCollection;
public HttpWebRequest WebRequest;
public HttpWebResponse WebResponse;
public int code { get; set; }
public string location { get; set; }
public string PostData(string url, string postData, string refer = "")
{
WebRequest = (HttpWebRequest)System.Net.WebRequest.Create(url);
WebRequest.UserAgent = UserAgent;
WebRequest.Referer = refer;
WebRequest.AllowAutoRedirect =false;
WebRequest.Timeout = 10000;
WebRequest.KeepAlive = true;
WebRequest.CookieContainer = new CookieContainer();
if (CookieCollection != null && CookieCollection.Count > 0)
{
WebRequest.CookieContainer.Add(CookieCollection);
}
WebRequest.ContentType = "application/x-www-form-urlencoded; charset=UTF-8";
WebRequest.Method = "POST";
try
{
var postBytes = Encoding.UTF8.GetBytes(postData);
WebRequest.ContentLength = postBytes.Length;
var postDataStream = WebRequest.GetRequestStream();
postDataStream.Write(postBytes, 0, postBytes.Length);
postDataStream.Close();
try
{
WebResponse = (HttpWebResponse)WebRequest.GetResponse();
this.code = (int)WebResponse.StatusCode;
this.location = WebResponse.Headers["Location"];
if (WebResponse.StatusCode == HttpStatusCode.OK ||WebResponse.StatusCode == HttpStatusCode.Redirect)
{
WebResponse.Cookies = WebRequest.CookieContainer.GetCookies(WebRequest.RequestUri);
if (WebResponse.Cookies.Count > 0)
{
if (CookieCollection == null)
{
CookieCollection = WebResponse.Cookies;
}
else
{
foreach (Cookie oRespCookie in WebResponse.Cookies)
{
var bMatch = false;
foreach (
var oReqCookie in
CookieCollection.Cast<Cookie>()
.Where(oReqCookie => oReqCookie.Name == oRespCookie.Name))
{
oReqCookie.Value = oRespCookie.Value;
bMatch = true;
break;
}
if (!bMatch)
CookieCollection.Add(oRespCookie);
}
}
}
var reader = new StreamReader(WebResponse.GetResponseStream());
var responseString = reader.ReadToEnd();
reader.Close();
return responseString;
}
}
catch (WebException wex)
{
if (wex.Response != null)
{
using (var errorResponse = (HttpWebResponse)wex.Response)
{
using (var reader = new StreamReader(errorResponse.GetResponseStream()))
{
var error = reader.ReadToEnd();
return error;
}
}
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
return "Error in posting data" ;
}
public string GetData(string url, string post = "")
{
var responseStr = string.Empty;
WebRequest = (HttpWebRequest)System.Net.WebRequest.Create(url);
WebRequest.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8";
WebRequest.Method = "GET";
WebRequest.KeepAlive = true;
WebRequest.Credentials = CredentialCache.DefaultCredentials;
WebRequest.UserAgent = UserAgent;
WebRequest.CookieContainer = new CookieContainer();
if (CookieCollection != null && CookieCollection.Count > 0)
{
WebRequest.CookieContainer.Add(CookieCollection);
}
if (!string.IsNullOrEmpty(post))
{
var postBytes = Encoding.UTF8.GetBytes(post);
WebRequest.ContentLength = postBytes.Length;
var postDataStream = WebRequest.GetRequestStream();
postDataStream.Write(postBytes, 0, postBytes.Length);
postDataStream.Close();
}
WebResponse wresp = null;
try
{
wresp = WebRequest.GetResponse();
var downStream = wresp.GetResponseStream();
if (downStream != null)
{
using (var downReader = new StreamReader(downStream))
{
responseStr = downReader.ReadToEnd();
}
}
return responseStr;
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
if (wresp != null)
{
wresp.Close();
wresp = null;
}
}
finally
{
WebRequest = null;
}
return responseStr;
}
}
Getting crfs-token:
private string GetToken()
{
string token = "";
String sourcestring = hc.GetData(loginURL);
Regex metaTag = new Regex(#"<meta[\s]+[^>]*?name[\s]?=[\s""']+(.*?)[\s""']+content[\s]?=[\s""']+(.*?)[""']+.*?>");
foreach (Match m in metaTag.Matches(sourcestring))
{
if (m.Groups[2].Value.Contains("token"))
{
continue;
}
token = m.Groups[2].Value;
}
return token;
}
Custom keyvaluepair
private string PostParam(Dictionary<string, string> data)
{
var sb = new StringBuilder();
var p = new List<string>();
foreach (KeyValuePair<string, string> pair in data)
{
sb.Clear();
sb.Append(pair.Key).Append("=").Append(pair.Value);
p.Add(sb.ToString());
}
var pp = string.Join("&", p);
return pp;
}
Login:
private HttpClient hc = new HttpClient();
Dictionary data = new Dictionary();
data.Add("utf8", "✓");
data.Add("authenticity_token", GetToken());
data.Add("plan", "");
data.Add("email", "email");
data.Add("password", "password");
hc.PostData(sessionURL,WebUtility.UrlEncode(PostParam(data)), loginURL);
Can someone tell me what I'm doing wrong? if I look the request header when trying to login to strava in browser its the same but still it doesn't log me.
I found the problem.
You need to encode only the token (and the UTF8 character), not the full post data.
This works for me (for some reason I need to run the code two times)
// First time says "logged out"
Dictionary<string, string> data = new Dictionary<string, string>();
data.Add("utf8", WebUtility.UrlEncode("✓"));
string token = GetToken();
string tokenEncoded = WebUtility.UrlEncode(token);
data.Add("authenticity_token", tokenEncoded);
data.Add("plan", "");
data.Add("email", "youremail");
data.Add("password", "yourpwd");
data.Add("remember_me", "on");
string parameters = PostParam(data);
hc.PostData(sessionURL, parameters, loginURL);
// Second time logs in
Dictionary<string, string> data = new Dictionary<string, string>();
data.Add("utf8", WebUtility.UrlEncode("✓"));
string token = GetToken();
string tokenEncoded = WebUtility.UrlEncode(token);
data.Add("authenticity_token", tokenEncoded);
data.Add("plan", "");
data.Add("email", "youremail");
data.Add("password", "yourpwd");
data.Add("remember_me", "on");
string parameters = PostParam(data);
hc.PostData(sessionURL, parameters, loginURL);
Note:
// Keep this on default value "true"
//WebRequest.AllowAutoRedirect = false;
remark: you can use this code (see my previous post) to change activity's status (privacy) after logging in:
Dictionary<string, string> data2 = new Dictionary<string, string>();
data2.Add("utf8", WebUtility.UrlEncode("✓"));
string token2 = GetToken();
string tokenEncoded2 = WebUtility.UrlEncode(token2);
data2.Add("_method", "patch");
data2.Add("authenticity_token", tokenEncoded2);
data2.Add("activity%5Bvisibility%5D", "only_me"); // or "followers_only"
string parameters2 = PostParam(data2);
hc.PostData("https://www.strava.com/activities/youractivityID", parameters2, loginURL);
I have an OpenIdConnect Server I'm connecting to an I would like to forward token data the first time logging in to be stored on the server. Currently I'm doing this to forward the access token
var xhr = new XMLHttpRequest();
xhr.open("GET", url);
xhr.onload = function () {
log(xhr.status, JSON.parse(xhr.responseText));
}
xhr.setRequestHeader("Authorization", "Bearer " + user.access_token);
xhr.send();
I want to send the Profile Data as well but I don't know the proper header.
How can I do something like this:
xhr.setRequestHeader("Authorization-Profile", "Bearer " + user.profile);
Does anyone know the proper header so I can add these claims to the access token.
Here is an example of what we did in one of our project:
Created a common API response class as below:
public class ApiCommonResponse
{
public object Object { get; set; }
public int httpStatus { get; set; }
public string httpErrorMessage { get; set; }
}
And a generic method to call GET and POST API endpoints. This method will map the response to the supplied data model and will return you the object.
public static ApiCommonResponse GetApiData<T>(string token, T dataModel, string apiEndPoint = null)
{
var responseText = "";
var apiCommonResponse = new ApiCommonResponse();
if (apiEndPoint != null)
{
var request = (HttpWebRequest)WebRequest.Create(apiEndPoint);
request.Method = "GET";
request.ContentType = "application/json";
request.Headers.Add("Authorization", "Bearer " + token);
request.Headers.Add("X-Api-Version", "");
try
{
var httpResponse = (HttpWebResponse)request.GetResponse();
var stream = httpResponse.GetResponseStream();
if (stream != null)
{
using (var streamReader = new StreamReader(stream))
{
responseText = streamReader.ReadToEnd();
}
}
}
catch (WebException we)
{
var stream = we.Response.GetResponseStream();
if (stream != null)
{
var resp = new StreamReader(stream).ReadToEnd();
dynamic obj = JsonConvert.DeserializeObject(resp);
throw new Exception(obj.ToString());
}
}
}
var jsonSettings = new JsonSerializerSettings { MissingMemberHandling = MissingMemberHandling.Ignore };
apiCommonResponse.Object = JsonConvert.DeserializeObject<T>(responseText, jsonSettings);
apiCommonResponse.httpStatus = 0;
return apiCommonResponse;
}
public static ApiCommonResponse PostApiData<T>(string username, string token, T dataModel, string apiEndPoint = null)
{
var apiCommonResponse = new ApiCommonResponse();
if (apiEndPoint == null) return null;
var webRequest = WebRequest.Create(apiEndPoint);
webRequest.Method = "POST";
webRequest.Timeout = 20000;
webRequest.ContentType = "application/json";
request.Headers.Add("Authorization", "Bearer " + token);
webRequest.Headers.Add("X-Api-Version", "");
using (var requeststreams = webRequest.GetRequestStream())
{
using (var sw = new StreamWriter(requeststreams))
{
sw.Write(JsonConvert.SerializeObject(dataModel));
}
}
try
{
var httpStatus = (((HttpWebResponse)webRequest.GetResponse()).StatusCode);
var httpMessage = (((HttpWebResponse)webRequest.GetResponse()).StatusDescription);
using (var s = webRequest.GetResponse().GetResponseStream())
{
if (s == null) return null;
using (var sr = new StreamReader(s))
{
var responseObj = sr.ReadToEnd();
if (!string.IsNullOrEmpty(responseObj))
{
apiCommonResponse = JsonConvert.DeserializeObject<ApiCommonResponse>(responseObj);
}
}
apiCommonResponse.httpStatus = (int)httpStatus;
apiCommonResponse.httpErrorMessage = httpMessage;
apiCommonResponse.Object = apiCommonResponse.Object;
}
}
catch (WebException we)
{
var stream = we.Response.GetResponseStream();
if (stream != null)
{
var resp = new StreamReader(stream).ReadToEnd();
dynamic obj = JsonConvert.DeserializeObject(resp);
throw new Exception(obj.ToString());
}
}
return apiCommonResponse;
}
Currently I'm building a c# based desktop application. Its like a small web form auto filler. The application make GET and POST calls for getting the images in responsestream. But I'm getting very slow response. Any suggestions on how I can increase the speed of image download. Following is the code for downloading the image and POST call
Image Download Function:
public void download_image(string go_to_url, string referer)
{
this.httpWebRequest_cls_1 = null;
HttpWebResponse response = null;
try
{
this.httpWebRequest_cls_1 = (HttpWebRequest)WebRequest.Create(go_to_url);
this.httpWebRequest_cls_1.Proxy = null;
WebRequest.DefaultWebProxy = null;
HttpRequestCachePolicy policy = new HttpRequestCachePolicy(HttpRequestCacheLevel.NoCacheNoStore);
this.httpWebRequest_cls_1.ServicePoint.UseNagleAlgorithm = false;
this.httpWebRequest_cls_1.ServicePoint.Expect100Continue = false;
this.httpWebRequest_cls_1.ServicePoint.ConnectionLimit = 100000;
this.httpWebRequest_cls_1.ServicePoint.ConnectionLeaseTimeout = 65000;
this.httpWebRequest_cls_1.ServicePoint.MaxIdleTime = 100000;
this.httpWebRequest_cls_1.CookieContainer = global_store.cookieContainer_0;
this.httpWebRequest_cls_1.Referer = str1;
this.httpWebRequest_cls_1.KeepAlive = true;
this.httpWebRequest_cls_1.ReadWriteTimeout = 0xc350;
this.httpWebRequest_cls_1.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8";
this.httpWebRequest_cls_1.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip,deflate");
this.httpWebRequest_cls_1.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.65 Safari/537.36";
this.httpWebRequest_cls_1.UnsafeAuthenticatedConnectionSharing = true;
this.httpWebRequest_cls_1.AuthenticationLevel = System.Net.Security.AuthenticationLevel.None;
this.httpWebRequest_cls_1.PreAuthenticate = true;
this.httpWebRequest_cls_1.AllowWriteStreamBuffering = false;
this.httpWebRequest_cls_1.Pipelined = false;
this.httpWebRequest_cls_1.ProtocolVersion = HttpVersion.Version11;
this.httpWebRequest_cls_1.ContentType = "application/x-www-form-urlencoded";
this.httpWebRequest_cls_1.Headers.Set("Cache-Control", "max-age=86400");
this.httpWebRequest_cls_1.ContentLength = 0L;
this.httpWebRequest_cls_1.Method = "GET";
response = (HttpWebResponse)this.httpWebRequest_cls_1.GetResponse();
global_store.image_0 = Image.FromStream(response.GetResponseStream());
staticstore.get_cap_time = new TimeSpan(DateTime.Now.Hour,
DateTime.Now.Minute, DateTime.Now.Second);
}
catch (WebException exception)
{
string exp = exception.ToString();
}
finally
{
if (response != null)
{
response.Close();
response = null;
}
if (this.httpWebRequest_cls_1 != null)
{
this.httpWebRequest_cls_1.Abort();
this.httpWebRequest_cls_1 = null;
}
}
}
POST Request Sending Function:
public string simple_web_call(string go_to, string from, string params_to_post, string conditions)
{
string str = "";
string responseHeader = "";
httpWebRequest_3 = null;
Stream requestStream = null;
HttpWebResponse response = null;
Stream responseStream = null;
try
{
this.httpWebRequest_3 = (HttpWebRequest)WebRequest.Create(new Uri(go_to));
HttpRequestCachePolicy policy = new HttpRequestCachePolicy(HttpRequestCacheLevel.NoCacheNoStore);
this.httpWebRequest_3.CachePolicy = policy;
this.httpWebRequest_3.Proxy = null;
WebRequest.DefaultWebProxy = null;
this.httpWebRequest_3.ServicePoint.UseNagleAlgorithm = false;
this.httpWebRequest_3.ServicePoint.Expect100Continue = false;
this.httpWebRequest_3.ServicePoint.ConnectionLimit = 65000;
this.httpWebRequest_3.ServicePoint.ConnectionLeaseTimeout = Class9.int_7;
this.httpWebRequest_3.ServicePoint.MaxIdleTime = 10000;
this.httpWebRequest_3.CookieContainer = Class28.cookieContainer_0;
this.httpWebRequest_3.Referer = from;
this.httpWebRequest_3.KeepAlive = true;
this.httpWebRequest_3.Connection = "keepalive";
this.httpWebRequest_3.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip;
this.httpWebRequest_3.UserAgent = "Mozilla/5.0 (Windows T 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.116 Safari/537.36";
this.httpWebRequest_3.Headers.Set("Cache-Control", "no-cache");
this.httpWebRequest_3.UnsafeAuthenticatedConnectionSharing = true;
this.httpWebRequest_3.AuthenticationLevel = System.Net.Security.AuthenticationLevel.None;
this.httpWebRequest_3.ProtocolVersion = HttpVersion.Version11;
this.httpWebRequest_3.Headers.Set("Cache-Control", "no-cache");
if (params_to_post != "")
{
this.httpWebRequest_3.Method = "POST";
byte[] bytes = Encoding.ASCII.GetBytes(params_to_post);
this.httpWebRequest_3.ContentType = "application/x-www-form-urlencoded";
this.httpWebRequest_3.ContentLength = bytes.Length;
requestStream = this.httpWebRequest_3.GetRequestStream();
requestStream.Write(bytes, 0, bytes.Length);
requestStream.Flush();
requestStream.Close();
}
else
{
this.httpWebRequest_3.Method = "GET";
}
response = (HttpWebResponse)this.httpWebRequest_3.GetResponse();
HttpWebResponse response2 = response;
switch (response2.StatusCode)
{
case HttpStatusCode.OK:
{
responseStream = response2.GetResponseStream();
str = new StreamReader(responseStream, Encoding.UTF8).ReadToEnd();
responseStream.Close();
responseStream = null;
goto flush_all;
}
case HttpStatusCode.MovedPermanently:
case HttpStatusCode.Found:
case HttpStatusCode.SeeOther:
case HttpStatusCode.TemporaryRedirect:
responseHeader = response2.GetResponseHeader("Location");
if (!responseHeader.Contains("err")) { break; }
str = "retry";
goto flush_all;
default:
str = "retry";
goto flush_all;
}
str = responseHeader;
flush_all:
response2 = null;
response.Close();
response = null;
this.httpWebRequest_3 = null;
if (str == "") { str = "retry"; }
}
catch (WebException exception)
{
string exp = exception.ToString();
}
finally
{
if (requestStream != null)
{
requestStream.Close();
requestStream.Dispose();
requestStream = null;
}
if (responseStream != null)
{
responseStream.Close();
responseStream.Dispose();
responseStream = null;
}
if (response != null)
{
response.Close();
response = null;
}
if (this.httpWebRequest_3 != null)
{
this.httpWebRequest_3.Abort();
this.httpWebRequest_3 = null;
}
}
return str;
}