I have tried to create a httpwebrequest for post request to api site. The header details below;enter image description here
and I have tried below code;
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create("https://www.etc.com/c/search");
//webRequest.Credentials = cache;
//webRequest.PreAuthenticate = true;
//header description
webRequest.Method = "POST";
webRequest.Connection = "keep-alive";
//webRequest.KeepAlive = true;
byte[] byteArray = Encoding.UTF8.GetBytes("{\"contains\":true,\"empName\":RAGS TO RACHES,\"injuryDate\":04/13/2018,\"state\":UT,\"stCd\":43}");
webRequest.Accept = "q=0.8;application/json;q=0.9";
webRequest.UserAgent = #"Mozilla/5.0 (Windows NT 6.3; Win64; x64; rv:59.0) Gecko/20100101 Firefox/59.0";
webRequest.ContentType = "application/json";
webRequest.ContentLength = byteArray.Length;
webRequest.Referer = "https://www.etc.com/c/search";
webRequest.Headers.Add("Accept-Encoding: gzip, deflate, br");
webRequest.Headers.Add("Accept-Language: en-US,en;q=0.5");
//webRequest.Headers.Add("Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3");
using (Stream webpageStream = webRequest.GetRequestStream())
{
webpageStream.Write(byteArray, 0, byteArray.Length);
}
Not getting resposne.
Related
Trying to POST a webrequest but not getting the actual results.. webrequest not getting submit. Previously this code was working fine in 2018. I'm not sure if they changed something at their end. I copied all the latest parameters, and other stuff from fiddler. Is there anyone please look into it as my deadline for the actual project is very tight and this code is just an integral part of the main project and was assuming it is functional already.. already spent few hours on this..
CookieContainer _cookies = new CookieContainer();
string urls = "https://ww3.freddiemac.com/loanlookup?Phone=&Email=&Contact=&lang=en&fname=TEST&lname=PERSON&strt_nbr=123&unit_num=&strt_name=FAKE STREET&strt_suffix=&addr_city=CHICAGO&addr_state=IL&addr_zip=60628&ssn=1234&Verify=Yes";
var request = (HttpWebRequest)WebRequest.Create(urls);
request.Host = "ww3.freddiemac.com";
request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36 Edg/83.0.478.61";
request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9";
WebHeaderCollection myWebHeaderCollection = request.Headers;
myWebHeaderCollection.Add("Accept-Language", "en-US,en;q=0.9");
request.Headers["Accept-Encoding"] = "gzip, deflate, br";
request.ContentType = "application/x-www-form-urlencoded";
request.Method = "POST";
request.Referer = "https://ww3.freddiemac.com/loanlookup";
var encoding = new ASCIIEncoding();
var postBytes = encoding.GetBytes(urls);
request.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip | DecompressionMethods.None;
request.ContentLength = postBytes.Length;
request.CookieContainer = _cookies;
request.KeepAlive = true;
var requestStream = request.GetRequestStream();
requestStream.Write(postBytes, 0, postBytes.Length);
requestStream.Close();
var response = (HttpWebResponse)request.GetResponse();
string html;
using (var reader = new StreamReader(response.GetResponseStream()))
{
html = reader.ReadToEnd();
}
if (html.Contains("No. Our records show"))
{
_qualified = false;
}
I tried to connect a REST API using the postman and it's always a good request. No problems.
But, in the rest implementation code I always receive the error "StatusCode: Unauthorized, Content-Type: text/plain; charset=utf-8, Content-Length: 0)".
I've tried many ways to do this but it never done.
//url = url server
//authorization = Bearer .....
//body = text json
var client = new RestClient(url);
var request = new RestRequest(Method.POST);
request.AddHeader("content-type", "application/json");
request.AddHeader("authorization", authorization);
request.AddParameter("application/json", body, ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
var result = response.Content;
In the postman
The server doesn't receive the Authorization token when I try to do it in the code.
I am using the HttpWebRequest but I think it's also possible using the RestClient.
I used the Fiddler to identify the headers in the postman request and then I reply this headers in the code.
The code below is working to me.
I will make some changes but that's it.
//url = url server
//authorization = Bearer .....
//body = text json
//bytesBody = body in byte[]
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
webRequest.PreAuthenticate = true;
webRequest.Method = "POST";
webRequest.Headers["Cache-Control"] = "no-cache";
webRequest.Accept = "*/*";
webRequest.Headers["Accept-Encoding"] = "gzip, deflate, br";
webRequest.Headers["Accept-Language"] = "en-US,en;q=0.9,pt-BR;q=0.8,pt;q=0.7";
webRequest.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36";
webRequest.ContentType = "application/json";
webRequest.ContentLength = bytesBody.Length;
webRequest.Headers["authorization"] = authorization;
//webRequest.Headers["Origin"] = "chrome-extension://fhbjgbiflinjbdggehcddcbncdddomop";
webRequest.KeepAlive = true;
webRequest.ServicePoint.Expect100Continue = false;
webRequest.Host = host;
using (Stream dataStream = webRequest.GetRequestStream())
{
dataStream.Write(bytesBody, 0, bytesBody.Length);
dataStream.Flush();
dataStream.Close();
}
WebResponse response = webRequest.GetResponse();
using (var streamReader = new StreamReader(response.GetResponseStream()))
{
string result = streamReader.ReadToEnd();
}
response.Close();
I want login to website and get html code after login successful.
https://viralstyle.com/login
Error: remote server returned an error 500 line: HttpWebResponse
webResp = (HttpWebResponse)req.GetResponse();
string email = "email#email.com";
string pw = "mypassword";
string PostData = String.Format("email={0}&password={1}&remember=0", email, pw);
CookieContainer cookieContainer = new CookieContainer();
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create("https://viralstyle.com/login");
req.CookieContainer = cookieContainer;
req.Method = "POST";
req.ContentLength = PostData.Length;
req.ContentType = "application/x-www-form-urlencoded";
req.AllowAutoRedirect = true;
req.UserAgent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.121 Safari/535.2";
ASCIIEncoding encoding = new ASCIIEncoding();
byte[] loginDataBytes = encoding.GetBytes(PostData);
req.ContentLength = loginDataBytes.Length;
Stream stream = req.GetRequestStream();
stream.Write(loginDataBytes, 0, loginDataBytes.Length);
HttpWebResponse webResp = (HttpWebResponse)req.GetResponse();
Stream datastream = webResp.GetResponseStream();
StreamReader reader = new StreamReader(datastream);
webBrowser1.DocumentText = reader.ReadToEnd();
After logged in, cookies are generated for subsequent requests.
I am trying to get cookies by sending a "POST" request.
In the response header, there is a "set-cookies" field and its value is like: "value1=value1,value2=value2......";
However, the CookieContainer i created for the HttpWebRequest is still empty.
So what is happening and how can I have my CookieContainer filled?
Any suggestion would be appreciated.
string loginData = "################################";
CookieContainer cookiecontainer = new CookieContainer();
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create("https://www.ftwilliam.com/cgi-bin/Enter.cgi");
myRequest.CookieContainer = cookiecontainer;
myRequest.Method = "POST";
myRequest.KeepAlive = true;
myRequest.AllowAutoRedirect = false;
myRequest.Referer = "https://www.ftwilliam.com/cgi-bin/Enter.cgi";
myRequest.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:26.0) Gecko/20100101 Firefox/26.0";
myRequest.ContentType = "application/x-www-form-urlencoded";
ASCIIEncoding encoding = new ASCIIEncoding();
byte[] loginDataBytes = encoding.GetBytes(loginData);
myRequest.ContentLength = loginDataBytes.Length;
Stream stream = myRequest.GetRequestStream();
stream.Write(loginDataBytes, 0, loginDataBytes.Length);
stream.Close();
HttpWebResponse res = (HttpWebResponse)myRequest.GetResponse();
Hi I am using a facebook posts feeding application which will read all the new posts from my facebook page and will save to my sharepoint list. Earlier, i.e, before June it has worked properly it feeds all the posts from fabebook to my Share point list. But nowadays its throws an error while getting response from the Facebook authorize url. I don't know what went wrong. Please help me if you guys have any suggestion to resolve this issue. I am appending my code part below.
private void btnSendToList_Click(object sender, EventArgs e)
{
try
{
if (ConfigurationSettings.AppSettings["fbClientID"] != null)
strClientID = ConfigurationSettings.AppSettings["fbClientID"];
if (ConfigurationSettings.AppSettings["fbRedirectURL"] != null)
strURL = ConfigurationSettings.AppSettings["fbRedirectURL"];
if (ConfigurationSettings.AppSettings["fbCltSecret"] != null)
strCltSecret = ConfigurationSettings.AppSettings["fbCltSecret"];
if (ConfigurationSettings.AppSettings["fbUserId"] != null)
strUserId = ConfigurationSettings.AppSettings["fbUserId"];
if (ConfigurationSettings.AppSettings["SPSiteURL"] != null)
strSiteURL = ConfigurationSettings.AppSettings["SPSiteURL"];
CookieCollection cookies = new CookieCollection();
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://www.facebook.com");
request.UserAgent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.121 Safari/535.2";
request.CookieContainer = new CookieContainer();
request.CookieContainer.Add(cookies);
//Get the response from the server and save the cookies from the first request..
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
cookies = response.Cookies;
string getUrl = "https://www.facebook.com/login.php?login_attempt=1";
string postData = String.Format("email={0}&pass={1}", "testuser#gmail.com", "test123$"); // Masking credentials.
getRequest = (HttpWebRequest)WebRequest.Create(getUrl);
getRequest.CookieContainer = request.CookieContainer;
getRequest.CookieContainer.Add(cookies);
getRequest.Method = WebRequestMethods.Http.Post;
getRequest.UserAgent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.121 Safari/535.2";
getRequest.AllowWriteStreamBuffering = true;
getRequest.ProtocolVersion = HttpVersion.Version11;
getRequest.AllowAutoRedirect = true;
getRequest.ContentType = "application/x-www-form-urlencoded";
byteArray = Encoding.ASCII.GetBytes(postData);
getRequest.ContentLength = byteArray.Length;
newStream = getRequest.GetRequestStream(); //open connection
newStream.Write(byteArray, 0, byteArray.Length); // Send the data.
getResponse = (HttpWebResponse)getRequest.GetResponse();
getRequest = (HttpWebRequest)WebRequest.Create(string.Format("https://graph.facebook.com/oauth/authorize?client_id={0}&redirect_uri={1}", strClientID, "http://www.facebook.com/connect/login_success.html"));
getRequest.Method = WebRequestMethods.Http.Get;
getRequest.CookieContainer = request.CookieContainer;
getRequest.CookieContainer.Add(cookies);
getRequest.UserAgent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.121 Safari/535.2";
getRequest.AllowWriteStreamBuffering = true;
//getRequest.ProtocolVersion = HttpVersion.Version11;
getRequest.AllowAutoRedirect = true;
getRequest.ContentType = "application/x-www-form-urlencoded";
getResponse = (HttpWebResponse)getRequest.GetResponse();
The above line throws WebExceptopn which is teeling like Process has timed out.
strAccessToken = getResponse.ResponseUri.Query;
strAccessToken = strAccessToken.Replace("#_=_", "");
strAccessToken = strAccessToken.Replace("?code=", "");
newStream.Close();
if (!string.IsNullOrEmpty(strAccessToken))
strCode = strAccessToken;
if (!string.IsNullOrEmpty(strCode) && !string.IsNullOrEmpty(strClientID) &&
!string.IsNullOrEmpty(strURL) && !string.IsNullOrEmpty(strCltSecret) &&
!string.IsNullOrEmpty(strUserId))
{
SaveToList();
}
}
catch (Exception ex)
{
LogError(ex);
}
}
Hi Finally i got the solution.
Here i am using getResponse = (HttpWebResponse)getRequest.GetResponse();
The problem is we can use only one HttpWebResponse at a time. In my case i am using the same object in two times without any disposing and closing. That's make me the error.
So I updated my code like this.
byteArray = Encoding.ASCII.GetBytes(postData);
getRequest.ContentLength = byteArray.Length;
newStream = getRequest.GetRequestStream(); //open connection
newStream.Write(byteArray, 0, byteArray.Length); // Send the data.
getResponse = (HttpWebResponse)getRequest.GetResponse();
getresponse.Close();
This solved my error. Thanks