I need to consume a restful webservice using .net 2.0 framework and not by using WCF. The RESTFul service is built in java and is designed only for POST operations. To consume the webservice in winforms in .net I am using a WebClient object.
Below is the sample code
string data="some data";
string url="some uri";
WebClient request = new WebClient();
//Application throws internal server error 500 at runtime when it hits this line.
string results = request.UploadString(url, "POST", data);
Is there anything wrong that i am doing here ? any help in this regard is greatly appreciated.
Are you sure that the service method in question accepts plain-text data and not something structured like JSON or XML/SOAP?
"Internal Server Error" is an indication that the Server has difficulty in processing the String you uploaded. There could be various causes to "Internal Server Error". Do you have access to the Server side code for debugging? One thing I can think of is the Encoding of the String you uploaded. You can probably try to set the Encoding property of WebClient to something else.
Related
I know, this question has been posted a couple of times before, but I didn't get a clear answer/find a solution. I'm simply using WebClient.DownloadString on a website that uses SSL. Whenever I run my program, I get a "404 not found" error. I tried my program on a website that doesn't use SSL, and it worked perfectly.
Here's my code:
System.Net.WebClient webClient = new System.Net.WebClient();
string webData = webClient.DownloadString("https://example.com?user=" + listBox1.Items[currentIndex]);
I'm trying to make my program compatible with websites that use SSL. Does anyone have any examples on how to do this? Thanks, all help is appreciated.
System.Net.WebClient.DownloadString() does support websites that use SSL. If the server returned a 404 error, that either means that the resource you're requesting doesn't exist or the web server is incorrectly configured to handle SSL requests as you desire.
I need to send financial messages(transactions) to a bank with the help of HTTPS communication. Bank server is written on Java while my application is in .Net.
Following are only details I received from their side:
Https request:
• The entire ISO request for https should be sent as an attachment to the following
servlet:
https:///Servlet/IBSOtherVendorRequestServlet
What are other components or documents required from their side and which namespace of .Net can I use for this communication?
Kindly help me on how to proceed.
You have to know how to query the servlet : SOAP ? REST ? what are the parameters and which method should be used ? result in plain text, XML or JSON ?
It looks like you have to POST a request to this Servlet. You have to know what is an "entire ISO request" then POST it to this url using HttpClient or HttpWebRequest. It will probably look like this : Upload files with HTTPWebrequest (multipart/form-data)
As part of learning node.js, I just created a very basic chat server with node.js and socket.io. The server basically adds everyone who visits the chat.html wep page into a real time chat and everything seems to be working!
Now, I'd like to have a C# desktop application take part in the chat (without using a web browser control :)).
What's the best way to go about this?
I created a socket server in nodejs, and connected to it using TcpClient.
using (var client = new TcpClient())
{
client.Connect(serverIp, port));
using (var w = new StreamWriter(client.GetStream()))
w.Write("Here comes the message");
}
Try using the HttpWebRequest class. It is pretty easy to use and doesn't have any dependencies on things like System.Web or any specific web browser. I use it simulating browser requests and analyzing responses in testing applications. It is flexible enough to allow you to set your own per request headers (in case you are working with a restful service, or some other service with expectations of specific headers). Additionally, it will follow redirects for you by default, but this behavior easy to turn off.
Creating a new request is simple:
HttpWebRequest my_request = (HttpWebRequest)WebRequest.Create("http://some.url/and/resource");
To submit the request:
HttpWebResponse my_response = my_request.GetResponse();
Now you can make sure you got the right status code, look at response headers, and you have access to the response body through a stream object. In order to do things like add post data (like HTML form data) to the request, you just write a UTF8 encoded string to the request object's stream.
This library should be pretty easy to include into any WinForms or WPF application. The docs on MSDN are pretty good.
One gotcha though, if the response isn't in the 200-402 range, HttpWebRequest throws an exception that you have to catch. Fortunately you can still access the response object, but it is kind of annoying that you have to handle it as an exception (especially since the exception is on the server side and not in your client code).
I have a WCF service hosted that is returning a byte[] of protobuf-net serialized data. Originally this was hosted over the default webHttpBinding settings (SOAP) and everything was working properly. I recently found out that I need to call HTTP GETs and POSTs directly from our client so I thought it'd be easier to switch to use a RESTful service. I switched to use the WebGet attribute and the WCF REST Template.
I attempted to create a simple web site to test the client and I'm having trouble deserializing the data. This is an example of how I'm calling the service:
using (WebClient client = new WebClient())
{
result = client.DownloadString(url);
}
// Deserialize
BinaryVehicles binVehs;
using (var ms = new MemoryStream(StrToByteArray(result)))
{
binVehs = Serializer.Deserialize<BinaryVehicles>(ms);
}
An example of what is returned in "result":
< base64Binary xmlns="http://schemas.microsoft.com/2003/10/Serialization/">ChsKCzEyMy00NTYtNzg5EgU0NDAwMBoFQmxhY2sKHAoLOTYzLTg1Mi03NDESBTIzMDAwGgZTaWx2ZXI=< /base64Binary>
I also attempted to deserialize the data between the < base64Binary > tags with no results. Does anyone have any idea on how I should go about sending the binary protobuf-net data from an WebGet method and how I should deserialize the data? Thanks.
protobuf-net primarily handles just the serialization aspects (the protobuf spec by Google doesn't define any further than this). So it really comes down to: how are you serializing it?
I must admit that the WCF GET approach is not something I've looked at hugely, so there is no special handling there. One simple approach may be to look at just returning a string, and handling the base-64 encoding yourself, but to be honest if you are doing HTTP GET, then WCF itself seems overkill.
I blogged here about using ASP.NET MVC at the server for protobuf via HTTP GET. The linked sample code also includes a wire-compatible ASP.NET implementation.
If there is something appropriate we can do to make WCF GET easier, I'm all ears...
how to consume php web service in c# Desktop application. I am doing this by adding web reference and through code
WebReference.TestWSDL pdl = new testingApp.WebReference.TestWSDL();
string copy = pdl.verify("testing");
but it throws the error
Possible SOAP version mismatch: Envelope namespace http://schemas.xmlsoap.org/wsdl/ was unexpected. Expecting http://schemas.xmlsoap.org/soap/envelope/.
Make sure you are sending the the appropriate soap version request that the service is expecting ie sending a soap 1.2 request to a service expecting a 1.1 request would give a similar error. Maybe run fiddler and post the messages that are sent and recieved for people to have a look at?