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
Related
First time using SOAP and wondering how can I make a simple SOAP request using django? I haven't yet tried setting up pysimplesoap, I first just want to make a connection to the webservice.
I have a string of the XML header and body
xml_header = ""
xml_body = ""
How can I send this request and wait for a response?
EDIT: I'm using Python 3.4 for SUDS is not an option
Another library worth checking out is Zeep.
Some of the more complex SOAP transactions are virtually impossible with SUDS, but are simple within ZEEP.
http://docs.python-zeep.org/en/master/
One way is to use the Suds library in your view: https://fedorahosted.org/suds/
Documentation: https://fedorahosted.org/suds/wiki/Documentation
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!
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);
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";
I use an IOC container which provides me with IService.
In the case where IService is a WCF service it is provided by a channel factory
When IService lives on the same machine it is able to access the same cookies and so no problem however once a WCF Service is called it needs to be sent those cookies.
I've spent a lot of time trying to find a way to send cookies using a channel factory and the only way I could find that works is the following
var cookie = _authenticationClient.GetHttpCookie();
HttpRequestMessageProperty httpRequestProperty = new HttpRequestMessageProperty();
httpRequestProperty.Headers.Add(HttpRequestHeader.Cookie, cookie.Name + "=" + cookie.Value);
using(var scope = new OperationContextScope((IClientChannel)_service))
{
OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name] = httpRequestProperty;
var result = _service.Add(details);
if (result.Result == RPGResult.Success)
{
return RedirectToAction("Index", "Home", result.Id);
}
}
The problem with me using that method is that I have to know that I'm calling a WCF Service which is not always the case. I've tried writing a wrapper for the ChannelFactory that opens a new operationcontextscope when it creates a new service and various other solutions but nothing has worked.
Anyone have any experience with sending cookies over WCF Services?
I found a solution involving using SilverLight, unfortunately I'm not using silverlight, the solution is here: http://greenicicleblog.com/2009/10/27/using-the-silverlight-httpclient-in-wcf-and-still-passing-cookies/
Unfortunately standard .net doesn't contain the IHttpCookieContainerManager interface
Ideally I would be able to use something similar,i.e. I would be able to tell the channelfactory to pass a cookie whenever it opened.
If anyone has a better way to pass a token that is used for authentication that would be appreciated too.
I have a solution where I create a proxy class of IService and then every time a method on IService is called it invokes the proxy created by the channel factory but the call itself is wrapped in an operationcontextscope just like the one I have in my question.
I used the proxy factory from this link http://www.codeproject.com/KB/cs/dynamicproxy.aspx