So I have a program which has to make a POST request. The request takes way too long! The code for this is:
string entity = "some text in here";
byte[] _entity = Encoding.UTF8.GetBytes(entity);
Stopwatch sw = new Stopwatch();
sw.Start();
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL);
request.Method = "POST";
request.ContentLength = _entity.Length;
request.UserAgent = UserAgent;
request.ContentType = "application/json";
request.Accept = "application/json";
request.Headers["CustomHeader"] = "value";
request.Headers["AnotherCustom"] = "valueAgain";
request.Headers["AnotherHeader"] = "value";
request.Headers["AnotherOne"] = "value";
request.Referer = "referer";
using (Stream stream = request.GetRequestStream())
{
stream.Write(_entity, 0, _entity.Length);
}
WebResponse response = request.GetResponse();
string responseString = string.Empty;
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
responseString = reader.ReadToEnd();
}
sw.Stop();
Console.WriteLine("took: " + sw.ElapsedMilliseconds);
When my program loads, I set these:
ServicePointManager.DefaultConnectionLimit = 1000;
ServicePointManager.UseNagleAlgorithm = false;
ServicePointManager.Expect100Continue = false;
WebRequest.DefaultWebProxy = null;
What other things can I do to increase the speed? The ping to the site that I am POSTing too is only 5ms, and I have a 10mb/s upload speed, yet the request takes around 400ms every time.
Would sending less headers work?
Related
I'm trying to get my program to talk to an API using the HttpWebRequest/Response. The POST call works and generates an auth code.
The second call I'm making is a GET call and for some reason it times out every time. I've tried Request.Timeout, .readwritetimeout, content type, servicepoint and protocolVersion with no success.
This is my code:
public void InHouseReservations()
{
try
{
int Id = 0;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create($"{Properties.Settings.Default.RMSAPIIp}reservations/inHouse?modelType=basic&propertyId=1");
//request.Method = "GET";
request.ContentType = "application/json";
request.Headers.Add($"authtoken: {RMSAuthToken}");
request.Timeout = 200000;
request.ReadWriteTimeout = 200000;
request.ServicePoint.Expect100Continue = false;
request.ProtocolVersion = HttpVersion.Version11;
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
Console.WriteLine(((HttpWebResponse)response).StatusDescription);
using (var dataStream = response.GetResponseStream())
{
StreamReader reader = new StreamReader(dataStream);
string responseFromServer = reader.ReadToEnd();
Console.WriteLine(responseFromServer);
}
var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
dynamic tokenJSON = JObject.Parse(responseString);
ReservationID = tokenJSON.Id;
response.Close();
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
How do I make the second request within that same connection?
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("String.url");
WebResponse response = request.GetResponse();
Stream stream = response.GetResponseStream();
StreamReader reader = new StreamReader(stream);
String result = reader.ReadToEnd();
stream.Dispose();
reader.Dispose();
HTTP web request makes a persistent connection..... u can use both "GET" or"POST"
you can increase a connection how much u want(eg 3 to 20 or 50 ...etc)
string webpageContent = "";
byte[] byteArray = Encoding.UTF8.GetBytes("value");
HttpWebRequest webRequest (HttpWebRequest)WebRequest.Create(URL);
webRequest.Method = "POST";
webRequest.KeepAlive = true;
webRequest.Timeout = 120000;
System.Net.ServicePointManager.DefaultConnectionLimit = 3;
webRequest.ContentType = "application/x-www-form-urlencoded";
webRequest.ContentLength = byteArray.Length;
using (Stream webpageStream = webRequest.GetRequestStream())
{
webpageStream.Write(byteArray, 0, byteArray.Length);
}
using (HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse())
{
using (StreamReader reader = new StreamReader(webResponse.GetResponseStream()))
{
webpageContent = reader.ReadToEnd();
}
}
The KeepAlive property on HttpWebRequest is used to persist connections. It defaults to true.
Here's the doc with more details:
https://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.keepalive(v=vs.110).aspx
I'm trying to get html code of authorizated steam page, but I can't log in.
My code is
public string tryLogin(string EXP, string MOD, string TIME)
{
var rsa = new RSA();
string encryptedPass;
rsa.Exponent = EXP;
rsa.Modulus = MOD;
encryptedPass = rsa.Encrypt(Pass);
string reqString = "username=kasyjack&password=" + encryptedPass + "&emailauth=&kasyjackfriendlyname=&captchagid=&captcha_text=&emailsteamid=&rsatimestamp=" + TIME + "&remember_login=false";
byte[] requestData = Encoding.UTF8.GetBytes(reqString);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://store.steampowered.com/login/dologin/");
request.ContentType = "application/x-www-form-urlencoded; charset=UTF-8";
request.ContentLength = requestData.Length;
request.Host = "store.steampowered.com";
request.Method = "POST";
request.UserAgent = Http.ChromeUserAgent();
request.CookieContainer = _CookieCont;
using (Stream st = request.GetRequestStream())
st.Write(requestData, 0, requestData.Length);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader sr = new StreamReader(response.GetResponseStream());
string RSAR = sr.ReadToEnd();
return RSAR;
}
but response message is
{"success":false,"requires_twofactor":false,"clear_password_field":true,"captcha_needed":false,"captcha_gid":-1,"message":"Incorrect login."}
So does anyone know, what's wrong with login? Need your help.
SteamBot repository on GitHub has a working example on how to login to Steam via their website: link to method.
Here is a diagram explaining the whole process(made by me):
I am bit stuck here, trying to Integrate ALM using Rest API from C# Window application . But failed at the first step that is authentication .
Here is my Authentication call :
public void auth(string url, string xml)
{
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
byte[] requestBytes = System.Text.Encoding.UTF8.GetBytes(xml.ToString());
req.Method = "POST";
req.ContentType = "application/xml";
req.Accept = "application/xml";
req.KeepAlive = true;
req.AllowAutoRedirect = true;
req.ContentLength = requestBytes.Length;
Stream requestStream = req.GetRequestStream();
requestStream.Write(requestBytes, 0, requestBytes.Length);
requestStream.Close();
HttpWebResponse res = (HttpWebResponse)req.GetResponse();
StreamReader sr = new StreamReader(res.GetResponseStream(), System.Text.Encoding.Default);
backstr = sr.ReadToEnd();
myheader = res.Headers.Get("Set-Cookie");
sr.Close();
res.Close();
}
which i am calling like this :
try
{
// my creds, server details
string server_ = #"https://MyAlmUrl";
string user_ = "username";
string password_ = "password";
string xmll = "<?xml version='1.0' encoding='utf-8'?><alm-authentication><user>" + user_ + "</user><password>" + password_ + "</password></alm-authentication>";
rest.auth(server_ + "/authentication-point/authenticate", xmll);
}
No matter what ever i do It lands into : 401 unauthorized error.
I do have a valid credentials as i am able to login using the web with same credentials .
Some one help me out, what i am doing wrong , As This ALM and using Rest API , both are new to me .
Try this :
HttpWebRequest myauthrequest = (HttpWebRequest)WebRequest.Create("http://{qcserver}/qcbin/authentication-point/alm-authenticate");
string AuthenticationXML = #"<alm-authentication>
<user>{qcserver-username}</user>
<password>{qcserver-pwd}</password>
</alm-authentication>";
byte[] Requestbytes = Encoding.UTF8.GetBytes(AuthenticationXML);
myauthrequest.Method = "POST";
myauthrequest.ContentType = "application/xml";
myauthrequest.ContentLength = Requestbytes.Length;
myauthrequest.Accept = "application/xml";
Stream RequestStr = myauthrequest.GetRequestStream();
RequestStr.Write(Requestbytes, 0, Requestbytes.Length);
RequestStr.Close();
HttpWebResponse myauthres = (HttpWebResponse)myauthrequest.GetResponse();
var AuthenticationCookie = myauthres.Headers.Get("Set-Cookie");
//request to get all domains in ALM
HttpWebRequest domainreq = (HttpWebRequest)WebRequest.Create("http://{qcserver}/qcbin/rest/domains/");
domainreq.Method = "GET";
domainreq.ContentType = "application/xml";
domainreq.Accept = "application/xml";
domainreq.Headers.Set(HttpRequestHeader.Cookie, AuthenticationCookie);
HttpWebResponse domainres= (HttpWebResponse)domainreq.GetResponse();
Stream RStream = domainres.GetResponseStream();
XDocument doc = XDocument.Load(RStream);
//request to dowmload a particular attachement assigned to a test case
HttpWebRequest attachmentreq = (HttpWebRequest)WebRequest.Create("http://{qcserver}/qcbin/rest/domains/{domainname}/project/{projectname}/tests/{testid}/attachments/file.xls");
attachmentreq.Method = "GET";
attachmentreq.ContentType = "application/xml";
attachmentreq.Accept = "application/octet-stream";
attachmentreq.Headers.Set(HttpRequestHeader.Cookie, AuthenticationCookie);
HttpWebResponse attachmentres= (HttpWebResponse)attachmentreq.GetResponse();
Stream RStream = attachmentres.GetResponseStream();
var fileStream = File.Create("C:\\PathToFile");
RStream.InputStream.CopyTo(fileStream);
fileStream.Close();
I'm trying to use C# to login to hotfile.com. The first big issue was to overcome the 417 Error, which this line solved it:
System.Net.ServicePointManager.Expect100Continue = false;
Now I'm getting this error as I try to login using POST:
You don't seem to accept cookies. Cookies are required in order to log in. Help
I've tried several times, and googled around and I still can't login to Hotfile.com.. My code is this:
string response;
byte[] buffer = Encoding.ASCII.GetBytes("user=XX&pass=XX");
CookieContainer cookies = new CookieContainer();
HttpWebRequest WebReq = (HttpWebRequest)WebRequest.Create("http://hotfile.com/login.php");
WebReq.Credentials = new NetworkCredential("XX", "XX");
WebReq.PreAuthenticate = true;
WebReq.Pipelined = true;
WebReq.CookieContainer = cookies;
WebReq.KeepAlive = true;
WebReq.Method = "POST";
WebReq.ContentType = "application/x-www-form-urlencoded";
WebReq.ContentLength = buffer.Length;
WebReq.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; SLCC1)";
Stream PostData = WebReq.GetRequestStream();
PostData.Write(buffer, 0, buffer.Length);
PostData.Close();
HttpWebResponse WebResp = (HttpWebResponse)WebReq.GetResponse();
Stream Answer = WebResp.GetResponseStream();
StreamReader _Answer = new StreamReader(Answer);
response = _Answer.ReadToEnd();
File.WriteAllText("dump.html", response);
Naturally, the user and pass would have your accounts values.
Here's a working example I wrote for you:
var cookies = new CookieContainer();
ServicePointManager.Expect100Continue = false;
var request = (HttpWebRequest)WebRequest.Create("http://www.hotfile.com/login.php");
request.CookieContainer = cookies;
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
using (var requestStream = request.GetRequestStream())
using (var writer = new StreamWriter(requestStream))
{
writer.Write("user=XX&pass=XX&returnto=/");
}
using (var responseStream = request.GetResponse().GetResponseStream())
using (var reader = new StreamReader(responseStream))
{
var result = reader.ReadToEnd();
Console.WriteLine(result);
}