How to pass variable to a web services in c#? - web-services

I am calling a drupal web services from C#. My web service in drupal accepts two parameters. When i am checking my Web service with chrome extension POSTMAN. ITs working fine and returning me the data. But I have no idea how to pass those two values in web service. I have written my code like this
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://careernet.localhost/rep-details/report_details/retrieve");
request.Method = "POST";
request.ContentType = "application/json";
request.Accept = "application/xml";
request.CookieContainer = new CookieContainer();
request.CookieContainer.Add(new Uri("http://careernet.localhost/rep-details/report_details/retrieve"), new Cookie("sid", sessid));
WebResponse response = request.GetResponse();
Stream responseStream = response.GetResponseStream();
// Open the stream using a StreamReader for easy access.
StreamReader reader = new StreamReader(responseStream);
string responseFromServer = reader.ReadToEnd();
The above code is works well if I don't have to pass any value and calling web service which does not accept any thing. But if I have to pass two values then how can I do it ?

quick google search for "httpwebrequest post" and first result is:http://msdn.microsoft.com/en-us/library/debx8sh9(v=vs.110).aspx

REST web services are nothing more than HTTP requests. When you want to send something using HTTP you send bytes which are not related with actual datatypes. This is why people very often use the JSON objects to describe arguments and results when using web services.
This post describes how you could make a POST HTTP request. In the step 7 you append some bytes in the request stream. This is where you should append your arguments.
If you see the full example (scroll down) you will see that the byte array appended to the stream is created using the following code snippet.
// Create POST data and convert it to a byte array.
string postData = "This is a test that posts this string to a Web server.";
byte[] byteArray = Encoding.UTF8.GetBytes (postData);
Since you only want to pass simple arguments and not objects, this would be sufficient. If you need to pass objects as arguments, I would suggest using JSON in the postData
string.
Hope I helped!

Related

c++ WebRequest with POST

I currently use this command to do simple get transfer between my c++ application and my PHP web server.
WebRequest ^wrGETURL;
wrGETURL = WebRequest::Create(sURL);
Stream ^objStream = wrGETURL->GetResponse()->GetResponseStream();
But I would like to create a POST request with the WebRequest class is it possible?
Yes, it's possible. Set the Method property of a RequestState instance linked to your WebRequest to "POST". See https://msdn.microsoft.com/en-us/system.net.webrequest.method.aspx

How to send Queryparam with JSON value using Jersey client in unit test

I am trying to write a test case for a jersey resource using InMemory container provided by Jersey.
As my service method contains many multivalued parameters as filters, I opted to send all of those values as single JSON parameter, so that it will be easy to send a list of values for each filter.
When i send the JSON string using target("path").queryParam("filters", jsonString).request().get(); the call fails die to Jersey clients internal query builder, which is parsing the url and checking for path param templates in the url. Since the url contains my JSON with "{" in it, they are interpreted as path param.
If I try to encode the JSON using URLEncode.encode(jsonStr, "UTF-8"), the path param template issue is solved but white spaces in JSON are received by server as "+" as jersey client encoding URL one more time, but server decoding it only once.
If I make the Queryparam as post param test is working, but i don't want to use POST for just to retrieve data.
I can't post original code due to company policies.
My question is, is there any way to disable path template check in jersey clieny by setting custom builder.
A simpler solution would be to replace the '+' by '%20' as suggested here and here:
URLEncode.encode(jsonStr, "UTF-8").replaceAll("\\+", "%20");

WSDL GUID data type not recognized in ColdFusion?

I am trying to consume the following web service from ColdFusion:
http://xserv.dell.com/services/assetservice.asmx
I should be able to consume the web service using the code below:
<cfscript>
params = structNew();
params.guid = "11111111-1111-1111-1111-111111111111";
params.applicationName = "test";
params.serviceTags = "JLJMHX1";
ws = createObject("webservice", "http://xserv.dell.com/services/assetservice.asmx?wsdl");
writeDump(ws)
ws.GetAssetInformation(params);
</cfscript>
The results of dumping out the WSDL information (ws), indicates that the GetAssetInformation method has the following signature:
getAssetInformation(com.microsoft.wsdl.types.Guid, java.lang.String, java.lang.String)
The service call errors every single time, saying that:
"Error Occurred While Processing Request
Web service operation GetAssetInformation with parameters {11111111-1111-1111-1111-111111111111,test,JLJMHX1} cannot be found."
I am sure this is due to the method expecting a "com.microsoft.wsdl.types.Guid" data type, but how can I pass that in via ColdFusion?
I can create and run the request in Fiddler with the same data and get a response back without issue, so there is something I am doing wrong in ColdFusion.
Any help would be appreciated.
The method expects a guid and two strings. You are passing a structure. Pass the arguments separately.
ColdFusion 10 also introduces Axis 2 for web services by default. For some web services, you need to use Axis 1 which you can enable in the ColdFusion Administrator. You will also need to refresh the web service.
createObject("webservice", theURL, {refreshWSDL=true,wsVersion=1})
I found the answer by following the instructions in this post:
Consume SOAP web service having complex types
Thanks for the help everyone!

RESTEasy Client Proxy Preemptive Basic Authentication

I am using RESTEasy Proxy Framework to call my Rest-Services. I would like to use preemptive authentication with the proxy framework.
Thats my current Code:
public void callSomeService() throws Exception {
RegisterBuiltin.register(ResteasyProviderFactory.getInstance());
DefaultHttpClient client = new DefaultHttpClient();
UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(
USERNAME, PASSWORD);
AuthScope authscope = new AuthScope(AuthScope.ANY_HOST,
AuthScope.ANY_PORT, AuthScope.ANY_REALM);
client.getCredentialsProvider().setCredentials(authscope, credentials);
ApacheHttpClient4Executor executer = new ApacheHttpClient4Executor(client);
dummyResource = ProxyFactory.create(DummyResource.class,
"http://localhost:8888/myapp/rest/", executer);
// Do some calls here
}
When I monitor the traffic of my application, the Rest-Service gets called twice:
First the client receives an 401 Error (UNAUTHORIZED)
In the second request there is the Authorization Header added and everything works
fine.
What I actually want to do is that the Authorization Header is already added in the first request! How can I do that?
I am using RESTEasy 2.3.5! I also read the documentation (http://docs.jboss.org/resteasy/docs/2.3.5.Final/userguide/html_single/index.html#transport_layer) where is an example given for preemptive authentication, which actually doesnt work, because of this code:
BasicScheme basicAuth = new BasicScheme();
authCache.put("com.bluemonkeydiamond.sippycups", basicAuth);
You're right, the example in the documentation does not compile. Try replacing the string "com.bluemonkeydiamond.sippycups" with an instance of HttpHost. The HttpHost class has several constructors so be sure to look at the JavaDocs. The simplest constructor takes a string. For example,
BasicScheme basicAuth = new BasicScheme();
authCache.put(new HttpHost("com.bluemonkeydiamond.sippycups"), basicAuth);

Problem with Posting JSON object with WSRequest

I want Play to call a webservice. The webservice accepts application/json and returns this as well. With the following code I'm trying to achieve this. (Note, the headers.put(xxx) are added later in an effort to solve the problem).
WSRequest request = WS.url(targetURL);
request.body = new Gson().toJson(user);
request.headers.put("Content-type","application/json");
request.headers.put("Accept","application/json");
request.post();
The strange thing is that my JBOSS server replies: "Cannot consume content type". If I use my 'Simple REST client' plugin in my Chrome browser, and provide the entire JSON Body GSon created and add the content-type header, I get a valid response. Is this not the way to send JSON to the server? Or am I missing some fundamental piece here?
While checking the API documentation on the WSRequest class i noticed the field mime-type
By setting it as follows JBOSS (resteasy) accepted my request succesfully.
request.mimeType = "application/json";