I'm trying to post to Feed -> SubmitFeed.
However the exception I receive back is:
you must pass a Content-MD5 HTTP header for your feed so we can be
sure it was not corrupted (e.g. dropped a 0 from a price) before we
process it
What I don't understand is the documentation states that it is optional so unsure why I am receiving this. Plus I am actually passing something through.
XML
<?xml version="1.0" encoding="UTF-8" ?>
- <AmazonEnvelope xmlns:xsd="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" schemaLocation="amzn-base.xsd">
- <Header>
<DocumentVersion>1</DocumentVersion>
<MerchantIdentifier>A1ddWU5JfffWJEddV9Xf668B</MerchantIdentifier>
</Header>
<MessageType>Product</MessageType>
<PurgeAndReplace>false</PurgeAndReplace>
- <Message>
<MessageId>1</MessageId>
<OperationType>Update</OperationType>
</Message>
- <Product>
<SKU>3308ff6-01</SKU>
</Product>
</AmazonEnvelope>
How I attach the Body/Header
var request = Helper.CreateWebRequest(xmlRequest.Config, genericparameters, signatureHelpers);
request.Headers.Add("x-amazon-user-agent", amazonHostAgent);
request.Method = "POST";
request.ContentType = amazonConfig.ContentType;
request.Timeout = 50000;
Helper.AddXmlDocumentToRequest(xmlRequest.XmlDocument.InnerXml, request);
public static void AddXmlDocumentToRequest(string xmlDocument, WebRequest request)
{
var bytes = Encoding.ASCII.GetBytes(xmlDocument);
request.ContentLength = bytes.Length;
var requestStream = request.GetRequestStream();
request.Headers.Add("Content-MD5", GetMd5HashFromStream(bytes));
requestStream.Write(bytes, 0, bytes.Length);
requestStream.Close();
}
public static string GetMd5HashFromStream(byte[] bytes)
{
using (var md5 = MD5.Create())
{
var hash = md5.ComputeHash(bytes);
return Convert.ToBase64String(hash);
}
}
Within the request when I drill down to Headers I have the following:
{x-amazon-user-agent: MWSApplication/1.0 (Language=c#)
Content-Type: text/xml
Host: mws.amazonservices.co.uk
Content-Length: 478
Expect: 100-continue
Content-MD5: 1xLxj5Y+WVsAVR9BbQfzQA==
}
When I run the xml through scratchpad it returns the same Content-MD5: 1xLxj5Y+WVsAVR9BbQfzQA==
Am I doing something wrong?
Thanks,
Clare
This might not be the answer for everyone however it was for me and if it helps one other person I will be happy :-)
Me being very stupid, added the header after the streaming, this is not what you should be doing.
Create the URL
Add headers and other elements i.e.
request.ContentType
Stream documents (body of request) if required
request.GetResponse.
Thanks,
Clare
Related
I'm using C#.
I'm trying to make a POST request with the following request body:
{"taskId":15, "values" : [{"Key": "myKey","Value":"<Items>
<Item>
<Item id="17362"/>
</Item>
</Items>"}]}
And the following code:
var postData = "{\"taskId\":15, \"values\" : [{\"Key\": \"myKey\",\"Value\":\"<Items> <Item> <Item id=\"17362\"/> </Item> </Items>\"}]}"
var data = Encoding.ASCII.GetBytes(postData);//postData is the reqeust body
request.Method = "POST";
request.ContentType = "application/json";
request.ContentLength = data.Length;
using (var stream = request.GetRequestStream())
{
stream.Write(data, 0, data.Length);
}
var response = (HttpWebResponse)request.GetResponse();
And got the following execption:
The remote server returned an error: (400) Bad Request
How can i solve it? Thanks!
Edit:
The web method signature is: MyWebMethod(int taskId, Dictionary<string, object> values)
And the both requests are works for me:
{"taskId":15, "values" : 123 }]}
{"taskId":15, "values" : "123" }]}
My Web Server is C# WebService (.asmx)
Problem solved by replace " to ".
The disadvantage is that i need to change my server side to replace again " to ".
I am attempting to send an attachment through a HttpWebRequest in a Console Application. After a few days of searching and scouring the internet for some understandable assistance on this, I came up with what I think is a decent solution from this site
While I think I've done everything correctly, I am receiving the following error:
Multipart stream ended before a terminating boundary was encountered.
Questions:
I'm hoping to get some assistance/guidance with the multipart error I'm receiving, as well as assistance in attaching the actual byte[] of the XML Document.
Requirements:
The data file that needs to be attached is is an XML file which
should be an MTOM Attachment. In order to make it Mtom, my
understanding is that I need to be sure that the messageEncoding
attribute of the <binding> element in the app.config should have
a value of "Mtom" and this will be encoded as such.
Based on the business requirements (of which is strict), I need to send the byte[] of the file, not simply the contents itself.
Web Request Method
private static HttpWebRequest CreateWebRequest(SoapAction action)
{
// Retrieve URL from Endpoint in the app.Config based on the action passed into the
// method.
string url = GetUrlAddress(action);
if (url != null)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip, deflate");
request.Headers.Add(HttpRequestHeader.ContentEncoding, "gzip");
request.Headers.Add("Content-Transfer-Encoding", "8bit");
request.Headers.Add("SOAPAction", action.ToString());
request.Method = "POST";
request.Headers.Add("MIME-Version", "1.0");
request.ContentType = "multipart/form-data; type=\"application/xop+xml;\" boundary=\"" + BoundaryMarker + "\"";
request.ClientCertificates.Add(SecurityCertificate.CertificateObject);
ServicePointManager.Expect100Continue = false;
return request;
}
else
{
throw new NullReferenceException("Address for Endpoint could not be resolved.");
}
}
Method to Submit Request
Based on this post of mine, I believe I am compressing the HttpWebRequest appropriately using GZip.
private static void SubmitRequest(HttpWebRequest request, XDocument soapXml, byte[] formXmlBytes, FileInfo fileToUpload)
{
using (Stream requestStream = request.GetRequestStream())
{
using (GZipStream gz = new GZipStream(requestStream, CompressionMode.Compress, false))
{
soapXml.Save(gz);
WriteToStream(gz, formXmlBytes, fileToUpload.Name);
}
}
}
Method used to Write the MIME information and attachment to the Stream
public static void WriteToStream(Stream stream, byte[] formData, string fileName)
{
// Write a new line to the stream.
byte[] newLineBytes = Encoding.UTF8.GetBytes("\r\n");
stream.Write(newLineBytes, 0, newLineBytes.Length);
// Write the header to the stream.
string header = String.Format(HeaderTemplate, BoundaryMarker, fileName, RequestContentID);
byte[] headerBytes = Encoding.UTF8.GetBytes(header);
stream.Write(headerBytes, 0, headerBytes.Length);
// Write a new line to the stream.
stream.Write(newLineBytes, 0, newLineBytes.Length);
// Write the formData to the stream.
stream.Write(formData, 0, formData.Length);
// Write a new line to the stream.
stream.Write(newLineBytes, 0, newLineBytes.Length);
// Write the footer to the stream.
byte[] boundaryFooterBytes = Encoding.UTF8.GetBytes("--" + BoundaryMarker + "--");
stream.Write(boundaryFooterBytes, 0, boundaryFooterBytes.Length);
}
Soap Body Snippet Element
By using Fiddler, I am able to see what the request actually looks like. To me, it appears that the attachment is actually being appended to the request as the XML that it is, instead of (what I thought would be) a byte[].
After this should be the byte[] of the attachment. Currently, the full XML document is displaying.
Accept-Encoding: gzip, deflate
Content-Transfer-Encoding: 8bit
MIME-Version: 1.0
Content-Type: multipart/form-data; type="application/xop+xml;" boundary="--b73acdd180274cab985e4d697bfde428"
Content-Length: 582081
Connection: Keep-Alive
<?xml version="1.0" encoding="utf-8"?>
<soapenv:Envelope xmlns:soapenv="..." ...>
...
<soapenv:Body>
<urn:Request version="1.0">
<urn:FileAttachment>cid:b73acdd180274cab985e4d697bfde428</urn:FileAttachment>
</urn:Request>
</soapenv:Body>
</soapenv:Envelope>
----b73acdd180274cab985e4d697bfde428
Content-Disposition: attachment; filename="test.xml"
Content-Type: text/xml
Content-Id: b73acdd180274cab985e4d697bfde428
<XML OF ATTACHMENT>
----b73acdd180274cab985e4d697bfde428--
I've been tasked to consume an XML RPC API, and I'm quite unfamiliar with it. I've glanced around a few pages describing it, and I've seen there's a few libraries out there as well.
However, it's left me with a feeling that all it comes down to is posting an HTTP request, similar to REST, except using XML in the body as opposed to JSON.
I imagine constructing an HTTP request similar to the below:
POST /path/service.cfm HTTP/1.0
From: example#example.com
User-Agent: whatever/1.0
Content-Type: application/xml
Content-Length: 155
<?xml version="1.0"?>
<methodCall>
<methodName>examples.getStateName</methodName>
<params>
<param>
<value><i4>40</i4></value>
</param>
</params>
</methodCall>
Am I on the right track? The abundance of libraries around give me the feeling that perhaps it's more complicated than what I thought. Ideally I'd like to write a quick client in .NET myself.
static void Main(string[] args)
{
HttpWebRequest request = BuildWebRequest();
var response = request.GetResponse() as HttpWebResponse;
var responseContent = new StreamReader(response.GetResponseStream()).ReadToEnd();
}
private static HttpWebRequest BuildWebRequest()
{
var request = WebRequest.Create(Url) as HttpWebRequest;
request.Method = "POST";
request.ContentType = "application/xml";
request.Timeout = 40000;
request.ServicePoint.Expect100Continue = true;
string body = #"<?xml version="1.0"?>
<methodCall>
<methodName>examples.getStateName</methodName>
<params>
<param>
<value><i4>40</i4></value>
</param>
</params>
</methodCall>";
byte[] bytes = Encoding.Default.GetBytes(body);
using (var requestStream = request.GetRequestStream())
{
requestStream.Write(bytes, 0, bytes.Length);
}
return request;
}
Yes it is not a problem, here is an example how to do it. In string body you must write only your xml and you should put the correct content type. If the service provider have requirements like specific Headers you should add them.
I've got a soap request I've written in http & javascript but I cannot seem to convert it into C# correctly.
Original: (Works)
<button onclick="doStuff()" type="submit">Send</button>
<textarea name="REQUEST_DATA" cols=120 rows=17 >
<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<createModifyRequest>
<username>josephs</username>
<lookupIds>
<lookupIds>4225</lookupIds><!--firepass-->
</lookupIds>
</createModifyRequest>
</soap:Body>
</soap:Envelope>
</textarea>
<script language="javascript">
function doStuff() {
var xmlhttp = new ActiveXObject("Msxml2.XMLHTTP.3.0");
xmlhttp.open("POST", "http://tpdev-itreq.transpower.co.nz:7777/usr/services/CreateModifyRequest", false);
xmlhttp.setRequestHeader("SOAPAction", "createModifyRequest");
var userpass = "josephs" + ":" + "pass";
xmlhttp.setRequestHeader("Authorization", "Basic " + (userpass));
xmlhttp.setRequestHeader("Content-Type", "text/xml");
xmlhttp.send(REQUEST_DATA.value);
}
Converted in C# (Does not work)
private void button1_Click(object sender, EventArgs e)
{
string soap =#"<?xml version=""1.0"" encoding=""utf-8""?>
<soap:Envelope xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance""
xmlns:xsd=""http://www.w3.org/2001/XMLSchema""
xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/"">
<soap:Body>
<createModifyRequest>
<username>josephs</username>
<lookupIds>
<lookupIds>4225</lookupIds>
<!--firepass-->
</lookupIds>
</createModifyRequest>
</soap:Body>
</soap:Envelope>";
HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://tpdev-itreq.transpower.co.nz:7777/usr/services/CreateModifyRequest");
req.Headers.Add("SOAPAction", "\"createModifyRequest\"");
var userpass = "josephs" + ":" + "pass";
req.Headers.Add("Authorization", "Basic " + (userpass));
// req.Headers.Add("Content-Type", "text/xml");
req.ContentType = "text/xml;charset=\"utf-8\"";
req.Accept = "text/xml";
req.Method = "POST";
using (Stream stm = req.GetRequestStream())
{
using (StreamWriter stmw = new StreamWriter(stm))
{
stmw.Write(soap);
}
}
WebResponse response = req.GetResponse();
Stream responseStream = response.GetResponseStream();
// TODO: Do whatever you need with the response
}
At the moment when I run the C# code I get an internal 500 server error, so what have I done wrong?
I have tried to reproduce your problem. Currently i am not able to create your request but i have generated local request with your data.
One thing I came to know is if I remove double quotes(") around the utf-8 and it worked fine.
Just pass charset=utf-8 instead of charset=\"utf-8\""
I am not sure it will work for you or not.
Is there a reason you can't just use Visual Studio's built in support for SOAP web services?
You can add a service reference or a web reference (depending on whch framework version you are targeting).
Then you can just use the proxy class that VS creates for you.
There's no advantage to writing all of the HTTP code yourself. Actually, there's a big disadvantage, in that you are aren't getting proper data types from the WSDL of your SOAP service.
I am trying to connect to the SalesForce.com bulk API so I can do mass uploads of data from my application. I have read through the PDF documentation which emphasizes using CURL to make the POST requests. In keeping with the instructions, I have created a text file in XML format which is used for logging into the server.
Login.txt contents below:
<?xml version="1.0" encoding="utf-8" ?>
<env:Envelope xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">
<env:Body>
<n1:login xmlns:n1="urn:partner.soap.sforce.com">
<n1:username>my username</n1:username>
<n1:password>my password</n1:password>
</n1:login>
</env:Body>
</env:Envelope>
Here is what I'm trying to do in my code to make the login request:
XmlDocument XMLResponse = null;
HttpWebRequest httpRequest;
HttpWebResponse httpResponse = null;
Stream requestStream = null;
Stream responseStream = null;
XmlTextReader xmlReader;
httpRequest = HttpWebRequest)WebRequest.Create("https://login.salesforce.com/services/Soap/c/20.0");
try
{
byte[] bytes = File.ReadAllBytes(filename);
httpRequest.Method = "POST";
httpRequest.ContentLength = bytes.Length;
httpRequest.ContentType = "text/xml; charset=UTF-8";
httpRequest.Headers.Add("SOAPAction: login");
requestStream = httpRequest.GetRequestStream();
requestStream.Write(bytes, 0, bytes.Length);
requestStream.Close();
httpResponse = (HttpWebResponse)httpRequest.GetResponse();
if (httpResponse.StatusCode == HttpStatusCode.OK)
{
responseStream = httpResponse.GetResponseStream();
xmlReader = new XmlTextReader(responseStream);
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load(xmlReader);
XMLResponse = xmldoc;
xmlReader.Close();
}
httpResponse.Close();
}
When this code executes I always get a 500 error. Does anyone have any experience in doing what I am attempting to do? Could you please provide me with some suggestions?
Thank you in advance.
For the login part, just download and import the partner WSDL and use the generated web service client. Otherwise, you'll want to update your code to read the response when it gets a 500 status code (the SOAP spec requires fault messages to use a 500 status code), the response body will give you more clues to the problem. I'd expect in this case you're getting an identity confirmation error, and you'll need to provide your api security token in addition to your password in the login request.
Generate the XML file to salesforce login
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<login xmlns="urn:enterprise.soap.sforce.com">
<username>username</username>
<password>password + token</password>
</login>
</soap:Body>
</soap:Envelope>
use the following c# code for login to salesforce
XmlDocument doc = new XmlDocument();
doc.LoadXml(str);
string uri = "https://login.salesforce.com/services/Soap/c/21.0";
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(uri);
req.Headers.Add("SOAPAction", "login");
req.ContentType = "text/xml;charset=\"utf-8\"";
req.Accept = "text/xml";
req.Method = "POST";
stm = req.GetRequestStream();
doc.Save(stm);
stm.Close();
WebResponse resp = req.GetResponse();
stm = resp.GetResponseStream();
XmlDocument doc1 = new XmlDocument();
doc1.Load(stm);