I am trying to make a webrequest in C# which is similar to that of CuRL in PHP. But I keep on getting issue of authentication. I wonder if the issue is in my credentials or the webrequest that I make. Following is the code for PHP CuRL request that I want to replicate:
$search = $searchurl . "type=player&start=$start&num=$count" . $searchstring;
//Set the cookie data
$cookie_string = $this->EASW_KEY."; ".$this->EASF_SESS ."; ".$this->PHISHKEY;
//Setup cURL HTTP request
$ch = curl_init($search);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_COOKIE, $cookie_string);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'x-http-method-override: GET',
$this->XSID)
);
//Contains the JSON file returned from EA
$EAPSEARCH = curl_exec($ch);
curl_close($ch);
This is what I have done so far to make it work in C#:
string data = this.easwKey + ";" + this.phishKey;
byte[] buffer = Encoding.ASCII.GetBytes(this.sid); //the data you want to send to the web service
HttpWebRequest WebReq = (HttpWebRequest)WebRequest.Create(urlPath);
WebReq.Method = "POST";
WebReq.ContentType = "application/json";
WebReq.ContentLength = buffer.Length;
WebReq.Headers["Cookie"] = data;
WebReq.Headers["X-HTTP-Method-Override"] = "GET";
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);
returnStr = _Answer.ReadToEnd();
The returnStr
always return code error of 401
, saying unauthorized..But I am able to login to the system using the same credentials..