WCF Client cant read "mutlipart/reoated" - c#

I have a simpel WCF-Client.
Binding
var binding = new WSHttpBinding(SecurityMode.Transport);
binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Certificate;
So now my Problem. The server respond with somthing like this:
--MIMEBoundary_4c0931c2662c48df1de58a689b82ce1eb3373535d903393a
Content-Type: text/xml; charset=UTF-8
Content-Transfer-Encoding: binary
Content-ID: <0.5c0931c2662c48df1de58a689b82ce1eb3373535d903393a#apache.org>
<?xml version="1.0" encoding="UTF-8"?>
<--rest of the response xml-->
--MIMEBoundary_4c0931c2662c48df1de58a689b82ce1eb3373535d903393a--
It looks like the normal respond is simply packed in the MIMEBoundary. I try to switch to MTOM. But i got this Error:
Error creating a reader for the MTOM message.
MTOM messages must have type 'application/xop+xml'
What could i do?

Related

How to send unencoded form data with C# HttpClient

I'm trying to "repurpose" a third-party API used by a desktop application. I've found that the below code gets me very close to matching the packets sent by the app:
var formData = new FormUrlEncodedContent(new[]
{
new KeyValuePair<string, string>(JsonConvert.SerializeObject(myPayload), "")
});
var response = Client.PostAsync(myURL, formData).Result;
var json = response.Content.ReadAsStringAsync().Result;
This gets me almost exactly the same payload sent by the application, except it encodes the data (I know, "encoded" is right there in the name). I need to get the exact same request but without the data being encoded, but I can't quite find the right object(s) to pull it off. How do I keep this payload from being URL encoded?
Edit:
This is a login request I pulled from Wireshark emanating from the application:
POST /Login HTTP/1.1
Content-Type: application/x-www-form-urlencoded
Host: 1.1.1.1
Content-Length: 161
Expect: 100-continue
Connection: Close
{"username":"myuser","auth-id":"0a0a140f81a2ce0c303386e93cec41bf04660c22a881be9a"}
This is what the above will generate:
POST /Login HTTP/1.1
Expect: 100-continue
Connection: Close
Content-Type: application/x-www-form-urlencoded
Content-Length: 221
Host: 1.1.1.1
%7B%22user-name%22%3A%22myuser%22%2C%22auth-id%22%3A%220a0a140f81a2ce0c303386e93cec41bf04660c22a881be9a%22%7D=
I've edited them for brevity so the Content-Length is wrong. I realize it might not be the best way to send this data, but I have no control over how it's consumed.
Since you're actually trying to send JSON, I think you need to wrap the JSON in a StringContent object rather than a FormUrlEncoded object. Form-encoded data and JSON data are two different ways of formatting a payload (another commonly used format would be XML, for example). Using them both together doesn't make any sense.
I think something like this should work:
var content = new StringContent(JsonConvert.SerializeObject(myPayload), Encoding.UTF8, "application/json");
var response = Client.PostAsync(myURL, content).Result;
var json = response.Content.ReadAsStringAsync().Result;
(P.S. the Content-Type: application/x-www-form-urlencoded header sent by the application appears to be misleading, since the request body clearly contains JSON. Presumably the receiving server is tolerant of this nonsense, or just ignores it because it's always expecting JSON.)

How to request and get the response in soap web services c#

I have the .asmx url that i need to get the order details by order number.
My problem is that when i execute the code it bring back the html design of 'http://domain.co.za/services/portal.asmx?op=GetOrderDefinition' without populating the response.
here is my code
var client = new RestClient("http://domain.co.za/services/portal.asmx?op=GetOrderDefinition");
var requests = new RestRequest(Method.GET);
client.Proxy = WebRequest.DefaultWebProxy;
client.Proxy.Credentials = System.Net.CredentialCache.DefaultCredentials;
requests.AddHeader("Host", "test.lensportal.co.za");
requests.AddHeader("Content-Type", "text/xml; charset=utf-8");
requests.AddHeader("SOAPAction", "http://www.domain.co.za/GetOrderDefinition");
requests.AddParameter("{\r\n \"orderRef\": \"" + LabOrderReference + "\"\r\n}", RestSharp.ParameterType.RequestBody);
IRestResponse responses = client.Execute(requests);
The link 'http://domain.co.za/services/portal.asmx?op=GetOrderDefinition' contain samples below and i need to pass 'orderRef' when requesting and get back list off order details. Please note that the link has also SOAP 1.1 design for response for now i just included the request part.
POST /services/portal.asmx HTTP/1.1
Host: test.lensportal.co.za
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://www.domain.co.za/GetOrderDefinition"
<?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>
<GetOrderDefinition xmlns="http://www.domain.co.za">
<orderRef>string</orderRef>
</GetOrderDefinition>
</soap:Body>
</soap:Envelope>

How to consume an MIME response attachment in C#?

I am working with a java-written web service called JasperServer. I would like to obtain a file from the web service and save it on my local.
The web service provides a get() method; it requests a String of XML, and returns a String, and the target file as a MIME attachment:
public string get(string requestXmlString)
Right now I try to use a string to take in the response:
String res2 = webServiceClient.get(xmlInput);
It gives me an Exception:
Client found response content type of 'multipart/related; type="text/xml"; start="<7817FB68F69B037F5A5DEDE2AC105A65>"; boundary="----=_Part_2_1089980294.1393857885100"', but expected 'text/xml'.
The request failed with the error message:
------=_Part_2_1089980294.1393857885100
Content-Type: text/xml; charset=UTF-8
Content-Transfer-Encoding: binary
Content-Id: <7817FB68F69B037F5A5DEDE2AC105A65>
So my question is how to consume an String response with MIME attachment in C# .Net. And how to save it to my local?
You need a component for parsing MIME format described by RFC 822/2045 and extensions.
.NET framework doesn't include built-in classes for that.
I've good experience with Mime4Net component (it is based on port from Apache mime4j):
Stream mimeMsgStream;
var m = new MimeMessage(mimeMsgStream);
MimeMessage provides DOM for MIME structure and attachment content could be easily extracted. Also note that Mime4Net is free only for non-commercial usage.
I have the same problem with consuming Java WS.
On WCF3 config I only add the messageEncoding propery to binding definition and set it to "Mtom".
Something like this:
...
<system.serviceModel>
<bindings>
<wsHttpBinding>
<binding name="e3" messageEncoding="Mtom">

Getting MIME type from Web Service Response header

I invoke a web service provided by an external partner company. The web service returns files (.pdf, .dox, .png, ...) as a byte array.
If I would need to get the Header information (in detail I am interested in the content-type data) from the code, how can I get this information?
On our side, we are using VS 2010 and C# as language.
Here the code:
var client = new PublicService();
wsRequest request = new wsRequest();
var docInfo = new documentInfo();
docInfo.documentId = HSdocumentID;
docInfo.position = 1;
request.documentInfos = { docInfo };
byte[] doc = client.deliver(deliverRequest); //returns the file as byte array
The RESPONSE header would look like:
<?xml version="1.0" ?>
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
<S:Body><ns2:deliverResponse xmlns:ns2="http://www.dalle.com/webservices/record/2012a">
<return>
<Include xmlns="http://www.w3.org/2004/08/xop/include"
href="cid:d3a#example.jaxws.sun.com"/>
</return></ns2:deliverResponse></S:Body></S:Envelope>
Content-Id: <d3a#example.jaxws.sun.com>
Content-Transfer-Encoding: binary
Content-Type: application/pdf <-- THIS IS THE INFO I NEED TO GET
Check out if there are SOAP headers on the Web Method call that answer your question
On the web method I do not have any properties/attributes that refer to the header. Is there a general way to get the Response header or is the web service that should provide features to get it?
(I provided an answer, rather than a comment, due to the code to be copied)

WCF Data Services access issues - 403 forbidden?

I have Silverlight 4.0 client calling WCF data service, in the service I have write allow to all (I know that is not very wise, but I want to test it first):
public static void InitializeService(DataServiceConfiguration config)
{
// TODO: set rules to indicate which entity sets and service operations are visible, updatable, etc.
// Examples:
config.SetEntitySetAccessRule("*", EntitySetRights.All);
config.SetServiceOperationAccessRule("*", ServiceOperationRights.All);
config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2;
config.UseVerboseErrors = true;
}
when I call the service from the client using BeginSaveChanges like this:
MyServiceEntity.BeginSaveChanges(SaveChangesOptions.Batch, OnChangesSaved, MyServiceEntity);
I receive forbidden error:
--batchresponse_a7bc1f95-8f8d-4e3b-9e24-108743499c3a
Content-Type: multipart/mixed; boundary=changesetresponse_04a92dd2-1fe4-4da5-8d2e-e020fe354f8f
--changesetresponse_04a92dd2-1fe4-4da5-8d2e-e020fe354f8f
Content-Type: application/http
Content-Transfer-Encoding: binary
HTTP/1.1 403 Forbidden
Content-ID: 1
DataServiceVersion: 1.0;
Content-Type: application/xml
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<error xmlns="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata">
<code></code>
<message xml:lang="en-US">Forbidden</message>
</error>
--changesetresponse_04a92dd2-1fe4-4da5-8d2e-e020fe354f8f--
--batchresponse_a7bc1f95-8f8d-4e3b-9e24-108743499c3a--
And Batch operation response code is 202, whatever this is supposed to mean.
Any help is appreciated.
I know its seems very obvious but, did you publish your service to production after you changed the right to allow write?
just make sure you are pointing to the correct service url.
regards.

Categories