Sent XML data to the server with Post method - c#

I am trying to send xml data to the web server, but when I sending the data I get error ,,System.Net.WebException: 'The remote server returned an error: (500) Internal Server Error.",.
here is my code:
public string postXMLData(string soap_action, string requestXml)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(#"https://secure.yieldplanet.com/XmlServices/ChannelManager.asmx");
request.Headers.Add(#"SOAPAction:XmlServices/" + soap_action);
request.ContentType = "text/xml; encoding='utf-8'";
request.Accept = "text/xml";
request.Method = "POST";
byte[] bytes;
bytes = System.Text.Encoding.ASCII.GetBytes(requestXml);
request.ContentLength = bytes.Length;
Stream requestStream = request.GetRequestStream();
requestStream.Write(bytes, 0, bytes.Length);
requestStream.Close();
HttpWebResponse response;
response = (HttpWebResponse)request.GetResponse();
if (response.StatusCode == HttpStatusCode.OK)
{
Stream responseStream = response.GetResponseStream();
string responseStr = new StreamReader(responseStream).ReadToEnd();
return responseStr;
}
return null;
}
enter image description here

Related

C# [XML2Array] Error parsing the XML string

i received an Error when i am posting a XML file via HttpWebRequest. I would like to show the response as an label.
My Code:
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https:xxxxxxxxxxxx");
byte[] bytes;
var file = Environment.SpecialFolder.MyDocuments + "\\myRequest.xml";
bytes = System.Text.Encoding.ASCII.GetBytes(file);
request.Headers.ToString();
request.ContentType = "application/xml; encoding='utf-8'";
request.ContentLength = bytes.Length;
request.Method = "POST";
Stream requestStream = request.GetRequestStream();
requestStream.Write(bytes, 0, bytes.Length);
requestStream.Close();
HttpWebResponse response;
response = (HttpWebResponse)request.GetResponse();
if (response.StatusCode == HttpStatusCode.OK)
{
Stream responseStream = response.GetResponseStream();
string responseStr = new StreamReader(responseStream).ReadToEnd();
label13.Text = responseStr;
}
My Response in label13.Text = The supplied data could not be imported in the stated format -[XML2Array] Error parsing the XML String

Issue In calling web api. Always returns 500 server error

I am making an HttpWeb request , "POST" to call url/api with basic authentication of username and password.It gives response when I use POSTMAN tool.But not sure why the .net code is giving me Internal server error.Below is what I am trying:
Note : The interchange data must be XML format.Hence I have provided text/xml
var requestXml = new XElement("transaction", new XElement("transactionId", "Dummy"));
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
byte[] bytes;
bytes = System.Text.Encoding.ASCII.GetBytes(XMLSerializer(requestXml));
request.ContentType = "text/xml; encoding=utf-8";
request.ContentLength = bytes.Length;
request.Method = "POST";
request.Headers.Add(HttpRequestHeader.Authorization, "Basic" + Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes("username:password")));
Stream requestStream = request.GetRequestStream();
requestStream.Write(bytes, 0, bytes.Length);
requestStream.Close();
HttpWebResponse response;
response = (HttpWebResponse)request.GetResponse();
if (response.StatusCode == HttpStatusCode.OK)
{
Stream responseStream = response.GetResponseStream();
string responseStr = new StreamReader(responseStream).ReadToEnd();
return responseStr;
}
Authorization header should be set to:
"Basic " + Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes("username:password")));
rather than
"Basic" + Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes("username:password")));
notice the extra space character after Basic.

Send an HTTP POST request with C#

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.

Unable to consume POST method of OpenShift REST based API

I am trying to create application into OpenShift Red hat environment from my sample code which I have developed in C#.net.
string url =string.Format("https://openshift.redhat.com/broker/rest/domain/{0}/applications", cbDomains.Text);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "POST";
request.Credentials = new NetworkCredential(USERNAME, PASSWORD);
string postData = "{\"name\": \"myapp\","
+ "\"cartridges\": [{"
+ "\"cartridge\": \"diy-0.1\"}],"
+ "\"scale\": false,"
+ "\"gear_size\": \"small\"}";
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
request.ContentType = "application/x-www-form-urlencoded;";
request.ContentLength = byteArray.Length;
Stream dataStream = request.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
dataStream.Close();
WebResponse response = request.GetResponse();
dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
string responseFromServer = reader.ReadToEnd();
reader.Close();
dataStream.Close();
response.Close();
when I am executing above code I am getting following error:
The remote server returned an error: (422) Unprocessable Entity.
on this below line:
WebResponse response = request.GetResponse();
Please let me where I am doing wrong. Thanks in advance Jyoti

C# 405 methods not allowed

I am posting data to a website and displaying the resulting json data,
This is the function I ended up with after taking some pieces from msdn's example
private string request(string url, string data)
{
byte[] byteArray;
string postData,
responseFromServer;
WebResponse response;
Stream dataStream;
StreamReader reader;
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
request.Proxy = new WebProxy(ip, port);
responseFromServer = string.Empty;
request.Method = "POST";
request.Timeout = 10000;
postData = data;
byteArray = Encoding.UTF8.GetBytes(postData);
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = byteArray.Length;
try
{
dataStream = request.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
dataStream.Close();
response = request.GetResponse();
dataStream = response.GetResponseStream();
reader = new StreamReader(dataStream);
responseFromServer = reader.ReadToEnd();
reader.Close();
dataStream.Close();
response.Close();
}
catch (Exception x)
{
x.ToString();
}
return responseFromServer;
}
}
The ip and port variables are within the class that this function is inside of.
When GetResponse() is called, it this exception.
{System.Net.WebException: The remote server returned an error: (405) Method Not Allowed.
Ive tested it and it is only because of the proxy, but the parameters are correct.
Why is this exception being thrown?

Categories

Resources