I have the following SOAP message that i want to post it to my WCF Service and get response.
"<s:Envelope xmlns:a=\"http://www.w3.org/2005/08/addressing\" xmlns:s=\"http://www.w3.org/2003/05/soap-envelope\">\r\n <s:Header>\r\n <a:Action s:mustUnderstand=\"1\">http://schemas.devleap.com/OrderService/IOrderService/InsertOrder</a:Action>\r\n <a:MessageID>urn:uuid:4cb619b7-365b-4108-880f-b302029d03c2</a:MessageID>\r\n <a:ReplyTo>\r\n <a:Address>http://www.w3.org/2005/08/addressing/anonymous</a:Address>\r\n </a:ReplyTo>\r\n </s:Header>\r\n <s:Body>\r\n <InsertOrder xmlns=\"http://schemas.devleap.com/OrderService\">\r\n <order xmlns:d4p1=\"http://schemas.devleap.com/OrderService/Order\" xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\">\r\n <d4p1:IdCustomer>2</d4p1:IdCustomer>\r\n <d4p1:IdOrder>46</d4p1:IdOrder>\r\n <d4p1:OrderItems xmlns:d5p1=\"http://schemas.devleap.com/OrderService/OrderItems\" xmlns:d5p2=\"http://schemas.devleap.com/OrderService/OrderItem\">\r\n <d5p1:OrderItem>\r\n <d5p2:IdProduct>P01</d5p2:IdProduct>\r\n <d5p2:Quantity>5</d5p2:Quantity>\r\n <d5p2:EuroPrice>20</d5p2:EuroPrice>\r\n </d5p1:OrderItem>\r\n <d5p1:OrderItem>\r\n <d5p2:IdProduct>P01</d5p2:IdProduct>\r\n <d5p2:Quantity>5</d5p2:Quantity>\r\n <d5p2:EuroPrice>20</d5p2:EuroPrice>\r\n </d5p1:OrderItem>\r\n </d4p1:OrderItems>\r\n </order>\r\n </InsertOrder>\r\n </s:Body>\r\n</s:Envelope>"
How can i post this SOAP to the WCF service?
I tried this but i get "The remote server returned an error: (500) Internal Server Error." exception.
using (var client = new WebClient())
{
client.Headers.Add("Content-Type", "application/soap+xml; charset=utf-8");
var response = client.UploadString("http://localhost:8000/OrderService", data);
}
If i remove the header from Envelope and add:
client.Headers.Add("Content-Type", "application/soap+xml; charset=utf-8");
client.Headers.Add("SOAPAction", "\"http://schemas.devleap.com/OrderService/IOrderService/InsertOrder\"");
i get the same error.
If i put
client.Headers.Add("Content-Type", "text/xml; charset=utf-8");
i get exception that server expects "application/soap+xml; charset=utf-8".
If someone knows how to call it right please help.
Thank you,
Adrya
After more digging i managed to post the SOAP to the service.
Here is code:
WebClient myWebClient = new WebClient();
myWebClient.Headers.Add("Content-Type", "application/soap+xml; charset=utf-8");
byte[] byteArray = Encoding.ASCII.GetBytes(postData);
byte[] responseArray = myWebClient.UploadData("http://localhost:8000/OrderService", "POST", byteArray);
Console.WriteLine("\nResponse received was {0}", Encoding.ASCII.GetString(responseArray));
Console.ReadKey();
Related
Trying to implement var client = new RestClient("https://bim360field.autodesk.com/fieldapi/checklists/v1/06eede44-a707-4f0b-9529-78abea6e6bf5");
var request = new RestRequest(Method.GET); https://bim360field.autodesk.com/apidoc/index.html#fieldapi/checklists/v1/checklists_api_method_2 in my C# solution.
I've set up an example Get Request in Postman. Works perfectly with the form-data, x-www-form-urlencoded, no auth, bearer token.
Tried to copy the exact Restshap Request code from Postman to my C# app. Does not work; always responds with "Unauthorized". Tried changing cookie mngmt, and auth window/nltm mngmt. Did not help.
var request = new RestRequest(Method.GET);
request.AddHeader("cache-control", "no-cache");
request.AddHeader("Connection", "keep-alive");
request.AddHeader("Content-Length", "91");
request.AddHeader("Accept-Encoding", "gzip, deflate");
request.AddHeader("Cookie", "OPTOUTMULTI_REF=b7027893-8564-4b33-8f1e-de0c175d38c6; OPTOUTMULTI_GEO=NL; utag_main=v_id:016639c85215001e1ab9f566c61201090006808800bd0$_sn:4$_ss:0$_st:1545042741235$ses_id:1545040922510%3Bexp-session$_pn:1%3Bexp-session; XSRF-TOKEN=YxM2BdzM19rML7OjKGZqcD0hAqv%2F225McEm8oB3m7No%3D; 75da74d446a8376e2cf6a286ff63573ab705f04c36540fdda21c1dbc1928d4e72cabf30a63e339690e88fc3801a93e44e6e3ddc20651f602d2c5eaaf49b21d61=f795a05c211085d994bf2e08996ff747");
request.AddHeader("Host", "bim360field.autodesk.com");
request.AddHeader("Postman-Token", "0b3c652d-462c-4aba-a661-476309792018,b4d4846c-a928-406f-837c-f16809a9bccd");
request.AddHeader("Cache-Control", "no-cache");
request.AddHeader("Accept", "*/*");
request.AddHeader("User-Agent", "PostmanRuntime/7.15.2");
request.AddHeader("Content-Type", "application/x-www-form-urlencoded");
request.AddHeader("content-type", "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW");
request.AddParameter("multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW", "------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"ticket\"\r\n\r\n{API_TICKET}\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"project_id\"\r\n\r\n049bf984-b8d1-4330-a17c-0832c6facf49\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);```
I expect that this code is also working with Restsharp. Do I miss anything?
For your cookies you will need to setup a CookieContainer on your Client.
Client = new RestClient(Request.GetEnvironment())
{
CookieContainer = new System.Net.CookieContainer()
};
You don't need the Postman Token. You will want to use the Bearer Token provided by your API to make the call and add it to your headers or parameters.
When intializing RestClient you only want to pass the base url. Then pass the rest of your api path during the RestRequest initialization. This will help for future use when wanting to hit more than one end point with the same cookies.
Here is a working example for you:
// (1) Build request
var client = new RestClient();
client.BaseUrl = new System.Uri("https://bim360field.autodesk.com/");
// Set resource or end point
var request = new RestRequest();
request.Resource = "/fieldapi/checklists/v1";
request.Method = Method.GET;
// Add parameters
request.AddParameter("ticket", "{API_TICKET}");
request.AddParameter("project_id", "049bf984-b8d1-4330-a17c-0832c6facf49");
// (2) Execute request and get response
IRestResponse response = client.Execute(request);
Hope it helps!
I am creating a console application in C# to check if URL is up and running using HttpWebRequest.
Note that the URL is generated by IBM's ICN (IBM Content Navigator). It is NOT hosted using IIS.
I am receiving the below error while trying to get HttpWebResponse from my C# console application.
Message: "The remote server returned an error: (500) Internal Server Error."
Status: ProtocolError
Below code is not working:
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(myurl);
request.Credentials = CredentialCache.DefaultCredentials;
request.Method = "GET";
request.Accept = "text/plain";
request.ContentType = "text/plain;charset=UTF-8";
request.Headers.Add("Accept-Language", "en-US");
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
if (response != null && response.StatusCode == HttpStatusCode.OK)
{
//Do some operations
}
else
{
//Do some operations
}
}
Please let me know if additional info. is required.
Any help or suggestions to solve this issue are appreciated.
Thanks in advance.
I am using HttpClient for rest service . At at one point I have a problem when I try to add "Content-Type" in my 'Get' request header .
I know "Content-Type" is suitable for content send in request body part But It's my need i have to send "Content-Type" with request header part.
I also try to remove "Content-Type" header from Invalid Header list of HttpRequestHeaders
I find link How do you set the Content-Type header for an HttpClient request?
Dim field = GetType(System.Net.Http.Headers.HttpRequestHeaders).GetField("invalidHeaders", System.Reflection.BindingFlags.NonPublic Or System.Reflection.BindingFlags.[Static])
If field IsNot Nothing Then
Dim invalidFields = DirectCast(field.GetValue(Nothing), HashSet(Of String))
invalidFields.Remove("Content-Type")
End If
But my issue not resolved I have exception
InnerException:
Message=The 'content-type' header must be modified using the appropriate property or method.
StackTrace:
at System.Net.WebHeaderCollection.ThrowOnRestrictedHeader(String headerName)
at System.Net.WebHeaderCollection.Add(String name, String value)
at System.Net.Http.HttpClientHandler.SetRequestHeaders(HttpWebRequest webRequest, HttpRequestMessage request)
at System.Net.Http.HttpClientHandler.CreateAndPrepareWebRequest(HttpRequestMessage request)
at System.Net.Http.HttpClientHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
InnerException:
I find some tools like Postman or SoupUI allow this .
Please help me to find some solution .
Thanks
HttpWebRequest may come to your rescue.
private static string CallService(string url)
{
WebRequest req = WebRequest.Create(url);
req.Method = "GET";
String json;
req.ContentType = "application/json; charset=utf-8";
var resp = req.GetResponse();
using (varstream = resp.GetResponseStream())
{
var re = new StreamReader(stream);
json = re.ReadToEnd();
}
return json;
}
}
Async implementation of this can be found from Getting the Response of a Asynchronous HttpWebRequest
If I send the http request through Postman it works and I get the result. But the same is not working and getting Unauthorized when I execute through RestSharp.
Below is the code snippet:
var client = new RestClient(
"http://Username:Password#localhost:port/_db/databaseName/_api/simple/all");
var request = new RestRequest(Method.PUT);
request.AddHeader("content-type", "application/json");
request.AddParameter("application/json",
"{\n \"collection\":\"collectionName\"\n}", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
return response;
According to the restsharp wiki you cannot specify the authentication parameters via the URL:
var client = new RestClient("http://example.com");
client.Authenticator = new SimpleAuthenticator("username", "foo", "password", "bar");
var request = new RestRequest("resource", Method.GET);
client.Execute(request);
in general its a good idea in such a case to drop https, and use wireshark or ngrep to inspect whats going on on the wire:
GET /_db/_system/_api/version?details=true HTTP/1.1
Host: 127.0.0.1
Connection: Keep-Alive
User-Agent: ArangoDB
Accept-Encoding: deflate
Authrization: Basic cm9vdDo=
to inspect the actualy generated authentication headers.
var client = new RestClient("http://example.com");
client.Authenticator = new SimpleAuthenticator("admin","admin");
var request = new RestRequest(Method.GET);
client.Execute(request);
This works for me..
I have a SOAP Envelop logged in Database using IDispatchMessageInspector. I need to invoke my WCF Service having wsHttpBinding using the SOAP Envlope.
I am using below code to invoke my WCF Service.
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create("ServiceURL");
req.Method = "POST";
string reqBody = theBomRequest; //it has entire soap
byte[] reqBodyBytes = Encoding.UTF8.GetBytes(reqBody);
req.ContentType = "application/soap+xml; charset=utf-8";
req.GetRequestStream().Write(reqBodyBytes, 0, reqBodyBytes.Length);
req.GetRequestStream().Close();
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
Console.WriteLine("HTTP/{0} {1} {2}", resp.ProtocolVersion, (int)resp.StatusCode, resp.StatusDescription);
if (resp.ContentLength > 0)
{
Console.WriteLine(new StreamReader(resp.GetResponseStream()).ReadToEnd());
}
resp.Close();
At GetResponse, I'm getting (500) Internal Server Error
Thanks,
Akif
Enable Tracing on your Service and see what is the cause for exception.