I want to send some values from my iPhone application to an ASp.Net web page in server. I am using asihttp for that. Its processing the request and invoking the page on server. But the none of the values are retrieved in server side. Below is the code from iPhone app.
NSURL *url = [NSURL URLWithString:urlString];
ASIFormDataRequest *request = [[[ASIFormDataRequest alloc] initWithURL:url] autorelease];
[request setPostValue:#"abc" forKey:#"from"];
[request setPostValue:#"abc" forKey:#"name"];
[request setPostValue:#"abc" forKey:#"phone"];
[request setDelegate:self];
[request startSynchronous];
On Server side I am using asp.net c#. THis is the code using for retriving values. But I am getting emtpy string?
sendMail(Request.QueryString["name"],Request.QueryString["from"],Request.QueryString["phone"]);
Could Someone help Please?
I don't think Request.QueryString["name"] will retrieve a post parameter. You either need to change your ASIHttpRequest to include the parameters in the query string, or modify your ASP.NET code to expect post parameters.
You could try Request.Form["name"], Request.Form["phone"], etc. on the server side.
Or, you could try:
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:#"%#?name=%#&...",urlString,name,...];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
on the client side.
QueryString is for values in the URL. A post will have values in Request.Form or just Request which will search through QueryString & Form for the values.
Another question with more detail about QueryString vs Form.
Request["key"] vs Request.Params["key"] vs Request.QueryString["key"]
Related
I'm trying to exchange data between a web page and a c# socket.
The c# socket is running on the localhost.
The webpage runs on a server and points to the localhost.
When the webpage sends a Get request to the c# socket an cross-domain error is shown.
XMLHttpRequest cannot load http://localhost:12345/. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://192.168.1.3:9000' is therefore not allowed access.
This is the JS running on the web page (Angular).
var serviceUrl = 'http://localhost:12345';
var service = $resource(
serviceUrl,{}, {
getCard: {
method: "GET"
}
}
);
service.getCard();
This is a part of the code from the c# console application.
private static void Send(Socket handler, String data)
{
// Convert the string data to byte data using ASCII encoding.
byte[] byteData = Encoding.ASCII.GetBytes(data);
// Begin sending the data to the remote device.
handler.BeginSend( byteData
, 0
, byteData.Length
, 0
, new AsyncCallback(SendCallback)
, handler
);
How can i add header information to the response.
The headers must be: Access-Control-Allow-Origin: *
It doesnt work when i add it in front of the string.
The header has to be added when you serve the page, not from the socket response. It's just a plain old HTTP header.
If you use IIS to serve your web page, do something like this before sending the page:
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*");
While sending the image to Web server along with data it is not accepting the lengthy strings. I am sending the data through url. its working good with smaller strings for country,continent and city
http://user.co/UserImage.svc/InsertObjectImage?UserId={UserId}&CategoryId={CategoryId}&ImageName={ImageName}&Gender={Gender}&Continent={Continent}&Country={Country}&City={City}
The above url am using in program upto "?", after that it is with params
NSString *url=[NSString stringWithFormat:#"http://userdata.co.in/UserImage.svc/InsertFacialImage?%#",requestString];
NSLog(#"insert facial image url : %#",url);
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init] ;
[request setURL:[NSURL URLWithString:url]];
[request setHTTPMethod:#"POST"];
The code I am using to send the image along with data.Here am passing strings with url.Objective-C NSString can hold the data up to 4.2Billion characters. In the web server the I made it to allow 200 characters for param. But when I am sending the lengthy string like united states of america its making the trouble not storing the data.Services developed in WCF using C#
There are length limitations to URLs but it is also worth checking that your URL is valid.
URLs need to be correctly encoded so you should be passing united%20states%20of%20america in your URL string rather than united states of america.
This is because URL encodes spaces to %20. For any other "special" characters these are also encoded. There are many online resource that should help you and a quick online encoder / decoder can be found here: http://meyerweb.com/eric/tools/dencoder/
I want using httpWebRequest to "POST" data to a website. So i used firebug analyzing what really send to server. First step, i use browser to browsing www.mytargetURL.net, second, turn on firebug, after that, i fill all form data and click the submit button (it's mean POST data to the server). So, I watching firebug and there was a lot of Parameters in request body part. Something like:
param1=
param2=
param3=default_value1
param4=default_value2
param5=value_I_set_byhand1
param6=value_I_set_byhand2
The question is: I should set up the request body of httpWebRequest obj with all the parameters i saw in firebug parameter table (it's mean all 6 parameters) or just parameters which has value (parameter 3-6) or just parameters i have filled in the submit form(just param5 and param6)?
Thanks you for all supports
You create the HttpWebRequest object, get the request stream, and write your parameters to it. The example at HttpRequest.GetRequestStream should point you in the right direction.
I am trying to upload data to a REST-Service via POST-Method, but for some reasons the server tells me:
System.Net.WebException: The remote server returned an error: NotFound.
I am trying to upload data with this code:
WebClient addserving = new WebClient();
addserving.Credentials = new NetworkCredential(username.Text, passwort.Password);
addserving.Encoding = System.Text.Encoding.GetEncoding("ISO-8859-1");
addserving.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
addserving.UploadStringAsync(new Uri("http://jokr.info/api/v8/diary/add_item.xml&apikey=123456&item_id=1240&serving_id=1566"), "POST");
addserving.UploadStringCompleted += new UploadStringCompletedEventHandler(serving_UploadStringCompleted);
The doc of the API tells me to post like this:
Rate Limit: Yes
HTTP Methods: POST
Authentication: Basic authentication (Username or E-Mail and Password)
Formats: xml
Parameters: format, apikey [GET], activity_id [POST], activity_duration [POST], activity_kj [POST], timestamp [POST] (optional)
Does anyone see what's wrong?
Shouldn't you have a ? before stating query parameters instead of &
http://jokr.info/api/v8/diary/add_item.xml?apikey=123456&item_id=1240&serving_id=1566
You're missing a question mark to mark the beginning of hte parameter collection.
Change
http://jokr.info/api/v8/diary/add_item.xml&apikey=123456&item_id
to
http://jokr.info/api/v8/diary/add_item.xml?apikey=123456&item_id
I'm trying to build an iPhone app and c# WCF Service to upload an image to a SQL Service database.
I've got my app breaking an image down to NSData and posting off to a WCF Service using the following code:
NSData *imageData = UIImageJPEGRepresentation(self.image, 90);
NSURL *url = [NSURL URLWithString:#"http://example.com/ImageDiaryService.svc/json/AddMediaItem"];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setPostValue:#"Test" forKey:#"Name"];
[request setPostValue:#"Test description." forKey:#"Description"];
[request setPostValue:#"JPEG" forKey:#"ImageType"];
[request setPostValue:#"iPhone" forKey:#"MediaType"];
[request setData:imageData withFileName:#"myphoto.jpg" andContentType:#"image/jpeg" forKey:#"ImageData"];
[request setDidFinishSelector:#selector(uploadFinished:)];
[request setDidFailSelector:#selector(uploadFailed:)];
[request setDelegate:self];
[request startAsynchronous];
The problem I'm having is with the web service. I'm not sure what type of data I should be receiving from the app POST. I've tried receiving it as an a byte array but that didn't working.
My WCF Service is a REST service.
Any help is much appreciated.
Try receiving it as a Stream. The blog post at http://blogs.msdn.com/b/carlosfigueira/archive/2008/04/17/wcf-raw-programming-model-receiving-arbitrary-data.aspx shows how to receive arbitrary data in a WCF REST service.