I am trying to get cognos report using siteminder token, below is my code.
string cognosUrl = "https://cognos.blah.com";
string reportPath = "/c10/cgi-bin/cognosisapi.dll/rds/reportData/report/"; string reportId = "ildjfsldkf"; //prod
cognosUrl += string.Concat(reportPath, reportId,"?blahblah");
string targetUrl = cognosUrl;
string strFormvalues = string.Concat("TARGET=",targetUrl);
ASCIIEncoding encoder = new ASCIIEncoding();
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(targetUrl);
byte[] data = encoder.GetBytes(strFormvalues);
request.AllowAutoRedirect = false;
request.Timeout = 120000;
request.ContentLength = data.Length;
request.ContentType = "text/xml;charset=\"utf-8\"";
request.Method = "POST";
request.Headers.Add(HttpRequestHeader.Cookie,"SMSESSION="+stoken);
request.GetRequestStream().Write(data, 0, data.Length);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
string json = reader.ReadToEnd();
byte[] byteArray = Encoding.UTF8.GetBytes(json);
MemoryStream restream = new MemoryStream(byteArray);
using (Stream output = File.OpenWrite(#"c:\\Projects\\Test_"+DateTime.Now.ToString("yyyyMMddHHmmssfff")+".txt"))
using (Stream input = restream)
{
if (input != null) input.CopyTo(output);
}
// var results = serializer.DeserializeObject(json);
reader.Close();
dataStream.Close();
response.Close();
But I am getting response as " DPR-ERR-2101 Your request was invalid.Please contact your administrator."
I am not using C# myself, but a few recommendations while debugging this:
If you are only posting a URL, why not use GET instead of POST?
Try paste the targetURL in your browser, see what happens.
In my setup, when I paste the URL in the browser, I am always redirected before I receive an answer. (to a URL like, /cgi-bin/cognosisapi.dll/rds/sessionOutput/conversationID/i292ED29A62474697AD44306A388F5BBB You are preventing that to happen, that might be an issue)
Hope that helps.
Related
Am doing an WEB API .net 4.62 that /Token with username password and grant_type to get access token once its generated i woul like to get it value of access_token.This does not happen but when i take the same code to a windows form aplication I am able to get it what could be wrong. It hangs on using (HttpWebResponse response = (HttpWebResponse)http.GetResponse())
try
{
string myParameters = "username=value1&password=value2&grant_type=password";
string baseAddress = "http://localhost:50128/token";
var http = (HttpWebRequest)WebRequest.Create(new Uri(baseAddress));
http.Accept = "application/x-www-form-urlencoded";
http.ContentType = "application/x-www-form-urlencoded";
http.Method = "POST";
string parsedContent = myParameters;
ASCIIEncoding encoding = new ASCIIEncoding();
Byte[] bytes = encoding.GetBytes(parsedContent);
Stream newStream = http.GetRequestStream();
newStream.Write(bytes, 0, bytes.Length);
newStream.Close();
http.ServicePoint.Expect100Continue = false;
http.ProtocolVersion = HttpVersion.Version11;
http.Timeout = 2000;
using (HttpWebResponse response = (HttpWebResponse)http.GetResponse())
{
var stream = response.GetResponseStream();
var sr = new StreamReader(stream);
var content = sr.ReadToEnd();
JObject json = JObject.Parse(content);
var message = json.SelectToken("access_token").ToString();
Console.Write(message);
}
}
catch (Exception ex)
{
//Handle the exceptions that could appear
}
change the using block like this and also convert your function to "async".
using (HttpWebResponse response = (HttpWebResponse)await Task.Factory.FromAsync(http.BeginGetResponse, http.EndGetResponse, null).ConfigureAwait(false))
let me know if it works.
I'm try to Send Data Using the WebRequest with POST But my problem is No data has be streamed to the server.
string user = textBox1.Text;
string password = textBox2.Text;
ASCIIEncoding encoding = new ASCIIEncoding();
string postData = "username" + user + "&password" + password;
byte[] data = encoding.GetBytes(postData);
WebRequest request = WebRequest.Create("http://localhost/s/test3.php");
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = data.Length;
Stream stream = request.GetRequestStream();
stream.Write(data, 0, data.Length);
stream.Close();
WebResponse response = request.GetResponse();
stream = response.GetResponseStream();
StreamReader sr99 = new StreamReader(stream);
MessageBox.Show(sr99.ReadToEnd());
sr99.Close();
stream.Close();
here the result
It's because you need to assign your posted parameters with the = equal sign:
byte[] data = Encoding.ASCII.GetBytes(
$"username={user}&password={password}");
WebRequest request = WebRequest.Create("http://localhost/s/test3.php");
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = data.Length;
using (Stream stream = request.GetRequestStream())
{
stream.Write(data, 0, data.Length);
}
string responseContent = null;
using (WebResponse response = request.GetResponse())
{
using (Stream stream = response.GetResponseStream())
{
using (StreamReader sr99 = new StreamReader(stream))
{
responseContent = sr99.ReadToEnd();
}
}
}
MessageBox.Show(responseContent);
See the username= and &password= in post data formatting.
You can test it on this fiddle.
Edit :
It seems that your PHP script has parameters named diffently than those used in your question.
With the following code, I'm trying to post a html form and expecting a complete html page-
string reqString = "destination=&calldate_from=2015-08-01&calldate_to=2015-08-02&call_status=ANSWERED&search=Search";
byte[] requestData = Encoding.UTF8.GetBytes(reqString);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://192.168.100.70:83/report/outgoing/search");
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = requestData.Length;
request.Host = "192.168.100.70:83";
request.Method = "POST";
Cookie idcookie = new Cookie("PHPSESSID", "19fv94lhgtdr80st6ig366q461") { Domain = "192.168.100.70" };
Cookie bdcookie = new Cookie("ciIPPBXOBd", "nMXEiUJiSU%3D") { Domain = "192.168.100.70" };
request.CookieContainer = request.CookieContainer ?? new CookieContainer();
request.CookieContainer.Add(idcookie);
request.CookieContainer.Add(bdcookie);
// Get the request stream.
Stream requestStream = request.GetRequestStream();
requestStream.Write(requestData, 0, requestData.Length);
// requestStream.Flush();
requestStream.Close();
WebResponse response = request.GetResponse();
var res = ((HttpWebResponse)response).StatusDescription; //gets OK here.
StreamReader reader = new StreamReader(response.GetResponseStream());
string returnValue = reader.ReadToEnd(); //found empty
reader.Close();
Code is running without throwing any exception. But I can not retrieve the full html. How can I do that?
My code:
HttpWebRequest httpWReq = (HttpWebRequest)System.Net.WebRequest.Create(domainUrl + "/Workarea/webservices/WebServiceAPI/User/User.asmx/GetAllUserGroups");
ASCIIEncoding encoding = new ASCIIEncoding();
string postData = "OrderBy=Id";
byte[] data = encoding.GetBytes(postData);
httpWReq.Method = "POST";
httpWReq.ContentType = "application/x-www-form-urlencoded";
httpWReq.ContentLength = data.Length;
using (Stream stream = httpWReq.GetRequestStream())
{
stream.Write(data, 0, data.Length);
}
HttpWebResponse response = (HttpWebResponse)httpWReq.GetResponse();
string responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
getXmlValue1(responseString);
Tested the above piece of code at my end (Ektron V9),and it's working fine.
The issue may be with the ektron site that you are referring.
Check whether the Ektron website to which you make the POST request is accessible or not.
I have an ordering website that needs to make a set up request on a supplier site.
For this i am using a WebHanlder (ashx file) to read the setup request in cXML using the HttpContext object which is working fine.
One of the requirements is that we send back a Cookie called "Buyer Cookie" along with a cXML 200 OK response.
The problem I am having is when I create a cookie in the context.Response it is not recieved in the ordering site when I do the response.Output.Write().
I have tried using response.Flush() after the write and this is still not working
How can I send a cookie back to the calling site?
Here is my code:
Ordering site
Stream stream = null;
byte[] bytes = Encoding.ASCII.GetBytes(File.ReadAllText(#"D:\Prototypes\HTTPPost\cXMLFiles\PunchOutSetupRequest.xml"));
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create("http://localhost:45454/PunchOutRequest.ashx");
webRequest.ContentType = "application/x-www-form-urlencoded";
webRequest.Method = "POST";
webRequest.ContentLength = bytes.Length;
try
{
stream = webRequest.GetRequestStream();
stream.Write(bytes, 0, bytes.Length);
}
catch (Exception)
{
throw;
}
finally
{
if (stream != null)
stream.Close();
}
HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse();
Stream responseStream = response.GetResponseStream();
StreamReader responseReader = new StreamReader(responseStream);
string r = responseReader.ReadToEnd();
var buy = webRequest.CookieContainer;
var buyer = response.Cookies["BuyerCookie"]; // This is always null
Supplier Site
var request = context.Request;
StreamReader reader = new StreamReader(request.InputStream);
string text = reader.ReadToEnd();
POSetup setup = new POSetup();
if (setup.IsSetupRequestValid(text))
{
HttpCookie cookie = new HttpCookie("BuyerCookie", "100");
context.Response.Cookies.Add(cookie);
context.Response.Output.Write(setup.GetOKResponse());
}
Try adding this line:
webRequest.CookieContainer = new CookieContainer();
right after
webRequest.ContentLength = bytes.Length;