Problems calling Magento SOAP V2 Service in C# - c#

I'm hosting a magento webshop on my local machine using IIS 7.5 and PHP 5.6 (testing). The shop is working just fine, but now I want to create a separate application using visual studio 2013. These are the steps that I've taken:
I've added the domain name "www.domain.com.local" to my hosts file directing to my localhost (www.domain.com.local -> 127.0.0.1)
I've created a new website on my IIS and added a new binding (www.domain.com.local - see 1)
I've added WinCache extension with the PHP Manager and set the PHP version to 5.6
Enable WS-I Compliance in the magento backend (System > Configuration > Magento Core Api)
Create a SOAP Role and User (Resource Access = All)
Open visual studio 2013 and create a new Console Application
Adding a new Service Reference (http://www.domain.com.local/index.php/api/v2_soap/?wsdl)
Trying to login - This is not working like it should be
Here is my piece of code:
class Program
{
static void Main(string[] args)
{
MainAsync(args).Wait();
}
static async Task MainAsync(string[] args)
{
using (var proxy = new Mage_Api_Model_Server_Wsi_HandlerPortTypeClient())
{
try
{
var loginResponse = await proxy.loginAsync("soap-admin", "xxxxxxxxx"); // api key
var sessionId = loginResponse.result;
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
Console.ReadLine();
}
}
}
And this is the error I'm getting:
The content type text/xml; charset=utf-8,text/xml; charset=utf-8 of the response message does not match the content type of the binding (text/xml; charset=utf-8). If using a custom encoder, be sure that the IsContentTypeSupported method is implemented properly. The first 297 bytes of the response were: '<?xml version="1.0" encoding="utf-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="urn:Magento">
<SOAP-ENV:Body>
<ns1:loginResponseParam>
<result>13fa067676759c3ce8ddd61c386b6d5c</result>
</ns1:loginResponseParam>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
'.
So as you can see, I'm getting my sessionId but keep getting this error. I've also used fiddler to investigate and getting a correct response: HTTP 200 OK. Does someone knows what the problem could be? Is it IIS related? localhost related?
(When I add the url as web reference it works just fine - old webservice method).
Related topics I've read and tried (without success):
C# SOAP - Error in deserializing body of reply message (Magento API)
C#+Magento API V2:The content type text/xml; charset=utf-8,text/xml; charset=UTF-8 of the response message does not match

If got this answer from Rian at Magento Stack Exchange. All credits go to Rian
The solution:
The problem you're experiencing is that .NET/C# is having trouble parsing the content type Magento is sending along with it's response. SOAP is notoriously finicky about receiving just the right stuff in just the right format. Couple that with PHP's rather poor implementation of the protocol and you're in for a lot of fun.
I'm looking at a Magento 1.9 for the following information:
After some digging I found that the header for the SOAP calls are set in app/code/core/Mage/Api/Model/Server/V2/Adapter/Soap.php on line 52.
51. ->clearHeaders()
52. ->setHeader('Content-Type','text/xml; charset='.$apiConfigCharset)
53. ->setBod...
Note that that Content-Type header matches your text/xml; charset=utf-8 desired charset. Adjusting line 52 to:
52. ->setHeader('Content-Type','text/xml; charset='.$apiConfigCharset, true)
tells Magento to force overwriting that header if it's already set.
Make sure to make a copy of the file with it's full path to the app/code/local/Mage/... to avoid overwriting core files. You'll thank me when you want to upgrade Magento at some point.
Also, make sure to look carefully, there's two setHeader() calls in that file.
And finally, there's also a WS-I compliant SOAP adapter available, the same fix applies to that file. You can find it in app/code/core/Mage/Api/Model/Server/Wsi/Adapter/Soap.php.

Related

consuming java web service in C# applications

I am going to develop a C# application using web service but the service is developed by Apache Axis2 and I can not handle the service to integrate adding service reference in C#. I get no response and only format exception.
I don't have service code, it's like ready-to-use web service, just give service reference and get started to use it.
The web service wsdl address is:
https://pttws.ptt.gov.tr/PttBilgi/services/Sorgu?wsdl
I also tried to run the service with SOAP UI but the XML response I get is like below:
<faultstring>The endpoint reference (EPR) for the Operation not found is /PttBilgi/services/Sorgu.SorguHttpSoap11Endpoint/ and the WSA Action = null. If this EPR was previously reachable, please contact the server administrator.</faultstring>
The raw response is like below, too which is but with some technical detail:
HTTP/1.1 500 Internal Server Error
Date: Fri, 14 Apr 2017 07:20:28 GMT
Server: gizli gizli gizli gizli
X-OPNET-Transaction-Trace: a2_5cc44d1b-0e0c-48a5-a44f-942377e9ab70
X-Powered-By: Servlet/2.5 JSP/2.1
Vary: Accept-Encoding,User-Agent
Connection: close
Transfer-Encoding: chunked
Content-Type: application/xml; charset=UTF-8
<faultstring>The endpoint reference (EPR) for the Operation not found is /PttBilgi/services/Sorgu.SorguHttpSoap11Endpoint/ and the WSA Action = null. If this EPR was previously reachable, please contact the server administrator.</faultstring>
It seems that content type has to be text/html to use it in C# apps but being of the service's content type is application/xml, so it can not convert to read and process the result.
How can I use the service with success, what do I have to do?
Thanks guys!
As described in this article at http://wso2.com/library/176/
If Axis2 engine cannot find a service and an operation for a message, it immediately fails, sending a fault to the sender.
If service not found - "Service Not found EPR is "
If service found but not an operation- "Operation Not found EPR is and WSA Action = "

HTTP 403 Server failed to authenticate the request when downloading Blob image from azure [duplicate]

I've search here and in Google but I can't find a solution.
With my C# code I want to read a file from Azure Storage Blob.
The code (only 6 line) works very well in another project (Windows 8.1 Universal App) but not in my new Windows 10 UWP App.
This is my code:
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(azureConnectionString);
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
CloudBlobContainer container = blobClient.GetContainerReference("container-name");
CloudBlob b1 = container.GetBlobReference("27.76914.json");
StorageFile file = await ApplicationData.Current.LocalFolder.CreateFileAsync("stefano1.json", CreationCollisionOption.ReplaceExisting);
await b1.DownloadToFileAsync(file);
The Exception:
Server failed to authenticate the request. Make sure the value of
Authorization header is formed correctly including the signature.
Using Fiddler4 I found this error on message 403:
The MAC signature found in the HTTP request 'R2t9hKsyXf470HF2LNP8T+M2nci0ddE/ojQ0r4UVjJQ=' is not the same as any computed signature
My attempts:
The application works the first time (file downloaded). The second time I get the Exception.
If I change the requested file name: the application works the first time (file downloaded). The second time I get the Exception.
The next day I get immediately the same exception (at first run)
Also if I delete bin and obj folders I get the error
If I create another application and try to download the same file (same as point 1), it works the first time, but not the second.
In a Console Application all works well.
This is the Fiddler4 Raw Request (where ***** is my Azure Storage Account Name):
GET https://*****.blob.core.windows.net/container-name/27.76914.json HTTP/1.1
x-ms-client-request-id: accee7e7-646d-417a-b734-1591cbc16a8d
x-ms-date: Thu, 03 Sep 2015 06:31:37 GMT
x-ms-version: 2015-02-21
User-Agent: WA-Storage/5.0.2 (Windows Runtime)
Authorization: SharedKey *****:R2t9hKsyXf470HF2LNP8T+M2nci0ddE/ojQ0r4UVjJQ=
Host: *****.blob.core.windows.net
If-Modified-Since: Sun, 30 Aug 2015 18:52:41 GMT
If-None-Match: "0x8D2B16C2ED82C4A"
Connection: Keep-Alive
Thank you!
This was also reported on our GitHub page here: https://github.com/Azure/azure-storage-net/issues/171
Our leading theory is that a caching proxy might be in between the client and the server affecting your requests.
We're still investigating and will let you know.
I've had a similar issue (in Java) trying to access blobs from local machine.
Trying to download a blob would work one out of two times, trying to get an InputStream never worked. In both cases the error was the same as yours.
My code always worked from a VM in the cloud.
The issue was fixed when I've changed the default protocol from HTTP to HTTPS.
When constructing your CloudStorageAccount, there is a constructor that allows you to specify the default protocol. It's also available for C# (here)
Also for the record, AZCopy will fail for a source with HTTP, with similar error.
You might give it a try.

Azure Storage: 403 Server failed to authenticate the request

I've search here and in Google but I can't find a solution.
With my C# code I want to read a file from Azure Storage Blob.
The code (only 6 line) works very well in another project (Windows 8.1 Universal App) but not in my new Windows 10 UWP App.
This is my code:
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(azureConnectionString);
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
CloudBlobContainer container = blobClient.GetContainerReference("container-name");
CloudBlob b1 = container.GetBlobReference("27.76914.json");
StorageFile file = await ApplicationData.Current.LocalFolder.CreateFileAsync("stefano1.json", CreationCollisionOption.ReplaceExisting);
await b1.DownloadToFileAsync(file);
The Exception:
Server failed to authenticate the request. Make sure the value of
Authorization header is formed correctly including the signature.
Using Fiddler4 I found this error on message 403:
The MAC signature found in the HTTP request 'R2t9hKsyXf470HF2LNP8T+M2nci0ddE/ojQ0r4UVjJQ=' is not the same as any computed signature
My attempts:
The application works the first time (file downloaded). The second time I get the Exception.
If I change the requested file name: the application works the first time (file downloaded). The second time I get the Exception.
The next day I get immediately the same exception (at first run)
Also if I delete bin and obj folders I get the error
If I create another application and try to download the same file (same as point 1), it works the first time, but not the second.
In a Console Application all works well.
This is the Fiddler4 Raw Request (where ***** is my Azure Storage Account Name):
GET https://*****.blob.core.windows.net/container-name/27.76914.json HTTP/1.1
x-ms-client-request-id: accee7e7-646d-417a-b734-1591cbc16a8d
x-ms-date: Thu, 03 Sep 2015 06:31:37 GMT
x-ms-version: 2015-02-21
User-Agent: WA-Storage/5.0.2 (Windows Runtime)
Authorization: SharedKey *****:R2t9hKsyXf470HF2LNP8T+M2nci0ddE/ojQ0r4UVjJQ=
Host: *****.blob.core.windows.net
If-Modified-Since: Sun, 30 Aug 2015 18:52:41 GMT
If-None-Match: "0x8D2B16C2ED82C4A"
Connection: Keep-Alive
Thank you!
This was also reported on our GitHub page here: https://github.com/Azure/azure-storage-net/issues/171
Our leading theory is that a caching proxy might be in between the client and the server affecting your requests.
We're still investigating and will let you know.
I've had a similar issue (in Java) trying to access blobs from local machine.
Trying to download a blob would work one out of two times, trying to get an InputStream never worked. In both cases the error was the same as yours.
My code always worked from a VM in the cloud.
The issue was fixed when I've changed the default protocol from HTTP to HTTPS.
When constructing your CloudStorageAccount, there is a constructor that allows you to specify the default protocol. It's also available for C# (here)
Also for the record, AZCopy will fail for a source with HTTP, with similar error.
You might give it a try.

Error posting to Amazon Web Services Kinesis with .NET SDK

I was hoping one of you could help me with the use of AWS Kinesis. I have been pouring over the documentation and I am still unable to post a "blob" of data to a Kinesis stream.
In the API the standard POST request is as follows.
POST / HTTP/1.1
Host: kinesis.<region>.<domain>
x-amz-Date: <Date>
Authorization: AWS4-HMAC-SHA256 Credential=<Credential>, SignedHeaders=contenttype;
date;host;user-agent;x-amz-date;x-amz-target;x-amzn-requestid, Signature=<Signature>
User-Agent: <UserAgentString>
Content-Type: application/x-amz-json-1.1
Content-Length: <PayloadSizeBytes>
Connection: Keep-Alive
X-Amz-Target: Kinesis_20131202.PutRecord
{
"StreamName": "exampleStreamName",
"Data": "XzxkYXRhPl8x",
"PartitionKey": "partitionKey"
}
Using "Postman" a google chrome app to make Http requests and filling in the appropriate information for the above fields I cannot for the life of me figure out how to make a successful data post to a stream. I was unable to do this successfully so i went on to do use the .NET SDK for visual studio.
I made a quick command line c# console application I am still having some issues.
My code:
http://pastebin.com/cyJeC0vU
The error message, System.Xml.XmlException was unhandled, appears on line 61 of the code: http://pastebin.com/HEG7DmMw
Has anyone had a successful experience using AWS Kinesis. I would love to pick your brain / repay you somehow for a bit of tutoring.
Thanks again for all of your help!
The error you're getting indicates that the response from the service was not able to be parsed. There is a clue in that the SDK switched from the JSON parser to the XML parser because the response looked like XML. This usually indicates that you are behind a proxy which requires authentication, and the proxy is giving you an HTML error message.
You can verify this by firing up a protocol analyzer like Fiddler and watching the request traffic. If your proxy requires credentials, here is some documentation about using the AWS SDK for .NET with proxies:
Configuring Credentials for Your AWS SDK for .NET Application

Importing Sales Orders from Magento Using C# (salesOrderList function): "There is an error in XML document (2, 372)"

Note: This question was originally part of Magento SOAP API V2 with C#: Issue with Stores that Require HTTP Authentication but I have moved a part of it here as a new question, as one part was already answered on that post.
ISSUE:
I have built a tool to import sales orders from customer stores (magento) for integration into our legacy order processing system. I have five stores to import from. Three work fine. Two of them have the following problem:
The following Exception is thrown upon executing the salesOrderList() call:
A first chance exception of type 'System.InvalidOperationException' occurred in System.Xml.dll
There is an error in XML document (2, 372).
The login succeeds, I am able to enumerate attribute values, etc.
But I am unable to run the salesOrderList() function with any
filter (and null too). It would be great if someone has experience
with this and can point me in the right direction, please.
Update: The invoking call is as follows:
salesOrderEntity[] soe = mservice.salesOrderList(mlogin, objSalesOrderFilterSet);
The Exception is, There is an error in XML document (2, 372).
The InnerException is, The specified type was not recognized: name='salesOrderListEntity', namespace='urn:Magento', at .
Fiddler showed the following requested and returned for the salesOrderList() call:
Requested:
POST /api/v2_soap/index HTTP/1.1
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; MS Web Services Client Protocol 4.0.30319.239)
VsDebuggerCausalityData: uIDPoz6RG9JzwkdBniF95/gqmAcAAAAAhgJHnbvB1UOTE1y4R1Iq5VGLcSLUxTNDg57BO/4OizgACQAA
Content-Type: text/xml; charset=utf-8
SOAPAction: "urn:Mage_Api_Model_Server_V2_HandlerAction"
Host: www.contoso.com
Content-Length: 1753
Expect: 100-continue
Returned:
POST http://www.contoso.com/api/v2_soap/index HTTP/1.1
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; MS Web Services Client Protocol 4.0.30319.239)
VsDebuggerCausalityData: uIDPoz6RG9JzwkdBniF95/gqmAcAAAAAhgJHnbvB1UOTE1y4R1Iq5VGLcSLUxTNDg57BO/4OizgACQAA
Content-Type: text/xml; charset=utf-8
SOAPAction: "urn:Mage_Api_Model_Server_V2_HandlerAction"
Host: www.contoso.com
Content-Length: 1753
Expect: 100-continue
<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:tns="urn:Magento" xmlns:types="urn:Magento/encodedTypes" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body soap:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><tns:salesOrderList><sessionId xsi:type="xsd:string">2f9c5bdc898fd92c1f61981147ee5495</sessionId><filters href="#id1" /></tns:salesOrderList><tns:filters id="id1" xsi:type="tns:filters"><complex_filter href="#id2" /></tns:filters><soapenc:Array id="id2" soapenc:arrayType="tns:complexFilter[3]"><Item href="#id3" /><Item href="#id4" /><Item href="#id5" /></soapenc:Array><tns:complexFilter id="id3" xsi:type="tns:complexFilter"><key xsi:type="xsd:string">created_at</key><value href="#id6" /></tns:complexFilter><tns:complexFilter id="id4" xsi:type="tns:complexFilter"><key xsi:type="xsd:string">created_at</key><value href="#id7" /></tns:complexFilter><tns:complexFilter id="id5" xsi:type="tns:complexFilter"><key xsi:type="xsd:string">status</key><value href="#id8" /></tns:complexFilter><tns:associativeEntity id="id6" xsi:type="tns:associativeEntity"><key xsi:type="xsd:string">from</key><value xsi:type="xsd:string">2011-12-28 00:00:00</value></tns:associativeEntity><tns:associativeEntity id="id7" xsi:type="tns:associativeEntity"><key xsi:type="xsd:string">to</key><value xsi:type="xsd:string">2011-12-28 23:59:59</value></tns:associativeEntity><tns:associativeEntity id="id8" xsi:type="tns:associativeEntity"><key xsi:type="xsd:string">in</key><value xsi:type="xsd:string">processing</value></tns:associativeEntity></soap:Body></soap:Envelope>
HTTP/1.1 200 OK
Date: Thu, 05 Jan 2012 12:11:19 GMT
Server: Apache/2.2.20 (Unix) mod_ssl/2.2.20 OpenSSL/1.0.0-fips mod_bwlimited/1.4 mod_fcgid/2.3.6
X-Powered-By: PHP/5.3.6
Content-Length: 6302
Content-Type: text/xml; charset=UTF-8
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="urn:Magento" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><SOAP-ENV:Body><ns1:salesOrderListResponse><result SOAP-ENC:arrayType="ns1:salesOrderListEntity[1]" xsi:type="ns1:salesOrderListEntityArray"><item xsi:type="ns1:salesOrderListEntity"><increment_id xsi:type="xsd:string">100001306</increment_id><store_id
...<more data>...
xsi:type="xsd:string">John</firstname><lastname xsi:type="xsd:string">Doe</lastname><telephone xsi:type="xsd:string">999-999-9999 ext. 3333</telephone><postcode xsi:type="xsd:string">11111</postcode></item></result></ns1:salesOrderListResponse></SOAP-ENV:Body></SOAP-ENV:Envelope>
This has an easy fix, though it might take some time and you need to change some code.
The problem: You can't use the Magento WSDL address if not less than 1.6.0, as it contains weird bugs that are fixed only with 1.6, read the release notes of each version for more information
The Solution: Remove all Service References from your project and create a proxy using the svcutil.exe utility pointing to a wsdl address that is from the most recent Magento (even if you install the latest version just for this, the latest stable to this date is 1.6.1).
Then add that generated project to your web/win project and use the service from it, and swap the final url in the Client object, for example:
MyNameSpace.MagentoSoapClient ws = new MyNameSpace.MagentoSoapClient();
ws.Endpoint.Address = new System.ServiceModel.EndpointAddress("http://domain.com/api/v2_soap");
give that a try.
OKay, here goes. The service reference in the original program was created by pointing to a wsdl address under one of our Magento Go stores. The program allowed dynamically changing the shop to work with, and so the URL was changed to the relevant shop URL appended with /api/v2_soap/index at runtime.
As explained in the question, the sales order import worked for some sites and failed for others. After some pretty good advice by balexandre, we used fiddler and some on-the-spot code as needed and figured out the issue.
The Problem Was: The service call salesOrderList() returns salesOrderEntity[] for Magento Go shops and salesOrderListEntity[] for 1.5.x shops. As the proxy was created using a Magento Go reference, the latter type was "unexpected" for the program, thereby causing the Exception There is an error in XML document (2, 372) with the innerException The specified type was not recognized: name='salesOrderListEntity', namespace='urn:Magento'. All the other methods that we had invoked from the web service worked though. But without sales order listing, they weren't much useful on their own.
An Intermediate Workaround Was: To tweak the wsdl file and reference.cs to declare 'salesOrderListEntityand to create an overloaded version ofsalesOrderListEntity()`. But this workaround created more problems than it solved.
The Final Solution Is: Create two different proxies by pointing to Magento Go and Magento 1.5.x. After switching to the actual shop URLs at runtime, use the proxy relevant for the current shop. Same calls and flow, except that the first proxy knows that it will receive salesOrderEntity[] for a salesOrderList() call and the second proxy knows that it will receive salesOrderListEntity[].
This works without issues and we have imported a few hundred sales orders as I write this. And it seems to work well for 1.6.1 shops as well. But I shall let you know if (God forbid) there be any issues on that front.
On a concluding note, my sincere thanks to balexandre for all the help extended.

Categories