C# [XML2Array] Error parsing the XML string - c#

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

Related

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.

Sent XML data to the server with Post method

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

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.

HttpWebRequest webservice call returns Request format is invalid: text/xml;charset=UTF-8

Trying to call WebService using HTTPWebRequest and posting data, it results in invalid request format, I have added http verbs post in both client and webservice, any ideas here ?
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("http://localhost/WS/test.asmx/GetData");
String xmlString = "Montreal";
ASCIIEncoding encoding = new ASCIIEncoding();
byte[] bytesToWrite = encoding.GetBytes(xmlString);
request.Method = "POST";
request.ContentLength = bytesToWrite.Length;
request.ContentType = "text/xml;charset=UTF-8";
Stream newStream = request.GetRequestStream();
newStream.Write(bytesToWrite, 0, bytesToWrite.Length);
newStream.Close(); //fails here with error message Request format is invalid: text/xml;charset=UTF-8.
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
string responseFromServer = reader.ReadToEnd();
Console.WriteLine(responseFromServer);
I got to modify the approach, this worked for me.
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("http://localhost/WS/test.asmx");
String xmlData = "Montréal";
String xmlString = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:tem=\"http://tempuri.org/\"><soapenv:Header/> <soapenv:Body><tem:GetData><tem:data>" + xmlData + "</tem:data></tem:GetData></soapenv:Body></soapenv:Envelope>";
byte[] bytesToWrite = Encoding.UTF8.GetBytes(xmlString);
request.Method = "POST";
request.ContentLength = bytesToWrite.Length;
request.ContentType = "text/xml;charset=UTF-8;";
Stream newStream = request.GetRequestStream();
newStream.Write(bytesToWrite, 0, bytesToWrite.Length);
newStream.Close();
try
{
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
string responseFromServer = reader.ReadToEnd();
Console.WriteLine(responseFromServer);
}
catch (Exception ex)
{
string msg = ex.Message;
}

Consume wcf service using HttpWebRequest c#

HttpWebRequest req = null;
HttpWebResponse res = null;
const string url = http://localhost/MyService/EService.svc/CreateMethod";
req = (HttpWebRequest)WebRequest.Create(url);
req.Method = "POST";
req.ContentType = "application/json; charset=utf-8";
req.Headers.Add("App", "Application");
ASCIIEncoding encoder = new ASCIIEncoding();
byte[] data = encoder.GetBytes("{ itemlist: 'sasfs' }");
req.ContentLength = data.Length;
res = (HttpWebResponse)req.GetResponse();
Stream responseStream = res.GetResponseStream();
var streamReader = new StreamReader(responseStream);
string txt = streamReader.ReadToEnd();
streamReader.Close();
streamReader.Dispose();
responseStream.Close();
responseStream.Dispose();
I have to use above code to consume service, but i am getting different errors-
1) You must provide a request body if you set ContentLength>0 ....
What is the code I am missing exactly here.
You missing few lines of code. You only setting ContentLength but you do not write content.
req.ContentLength = data.Length;
//Write request data(setting content of request)
Stream reqStream = req.GetRequestStream();
reqStream.Write(data, 0, data.Length);
reqStream.Close();
res = (HttpWebResponse)req.GetResponse();

Categories

Resources