How can I post a request using HttpPost from android to Asp.Net page?
I use such that
HttpPost request = new HttpPost(m_WebRequestUrl);
HttpParams params = new BasicHttpParams();
params.setParameter("Test", "Test");
request.setParams(params);
client.setParams(params);
response = client.execute(request);
Regards
Shibu
Whether Asp.Net or Php etc. doesnt matter .On server side you have to have a service that handles your request and responses in appropriate way.
Your request is good to go.
Related
Our client wants to see some information in POST request headers we are sending, equivalent to what browsers send. Here's a server log for illustration:
[01/Sep/2013:23:11:57 +0100] "POST /script.php?par=test HTTP/1.1" 200 4 "-" "-"
And it should be like this:
[01/Sep/2013:23:11:57 +0100] "POST /script.php?par=test HTTP/1.1" 200 4 "appVer 1.0" "OS version 4.3"
I'm using HttpPost class and some script that wrapps SSL around HttpClient for HTTPS so I'd like to stay with current implementation.
To add this info I tried
HttpPost post = new HttpPost(uri.toASCIIString());
post.setHeader("appVersion", appVer);
and
HttpParams httpParams = new BasicHttpParams();
httpParams.setParameter("appVersion", appVer);
HttpClient client = new DefaultHttpClient(httpParams);
without success. How do I do this?
i am trying to create an application on android phone that takes username and password from user, encrypt the password using md5 then connect to url with these parameters.
a code to connect worked fine on iphone, but i couldn't find something like it in android:
NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:myURL];
[request setHTTPMethod:#"GET"];
[request addValue:usernameField.text forHTTPHeaderField:#"UserName"];
[request addValue:MD5Pass2 forHTTPHeaderField:#"Password"];
i tried to connect via httpurlconnection used post/get Dataoutputstream, httpclient send parameters httpget/httppost, but no success.
I think I need to send the parameters as headerfield but I don't know how.
note: I compared encryption results and it was correct.
I find the Apache library to be much more straightforward when it comes to HTTP.
An example of this would be as follows:
DefaultHttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet();
request.setURI(new URI("http://www.internet.com/api"));
Normally with a GET request you would use GET parameters, ie append them to the end of the URL like so:
String url = "http://www.internet.com/api?UserName=YourUsername&Password=yourpassword"
request.setURI(new URI(url));
But since you specified you want them as headers you could:
request.addHeader("UserName", username);
request.addHeader("Password", password);
and then:
HttpResponse response = client.execute(request);
//Parse the response from the input stream object inside the HttpResponse
you can try using HttpUrlConnection with this http://developer.android.com/reference/java/net/URLConnection.html#addRequestProperty%28java.lang.String,%20java.lang.String%29
how to upload the datas to webserver from android mobile.Please provide coding
I think this compiles:
HttpPut request = new HttpPut(<uri>);
request.setEntity(new ByteArrayEntity(<your data>));
HttpResponse response = HttpClient.execute(httpPut);
You might want to use the HttpPost instead of HttpPut and also specify the content type on the request.
I am able to use the example here: http://www.androidsnippets.org/snippets/36/index.html and successfully get the "HTTP/1.1 OK" response for a webesite I am sending the HttpPost along with the user credentials. However, I am unable to use an HttpGet to further browse other pages on this site.
Can anyone please let me know, what's going wrong. I am sorry - I am very new to Java.
My guess would be that when the website gets the Post and logs the user in, it sets cookies on the response to indicate that the user is logged in, and then requires those cookies on subsequent Get's.
You will need to do something like the following (this is borrowed from a bigger app so may not compile right out of the box)
DefaultHttpClient mHttpClient = new DefaultHttpClient();
BasicHttpContext mHttpContext = new BasicHttpContext();
CookieStore mCookieStore = new BasicCookieStore();
mHttpContext.setAttribute(ClientContext.COOKIE_STORE, mCookieStore);
This sets up a cookie store within the HTTP context, and you then use that context on Get's and Post's. For example...
HttpResponse response = mHttpClient.execute(mRequest, mHttpContext);
Under the covers the HTTP client will store cookies from responses, and add them to requests.
Update:
Found the answer myself, see below :-)
Hi,
I'am currently coding an android app that submits stuff in the background using HTTP Post and AsyncTask. I use the org.apache.http.client Package for this. I based my code on this example.
Basically, my code looks like this:
public void postData() {
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://192.168.1.137:8880/form");
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("id", "12345"));
nameValuePairs.add(new BasicNameValuePair("stringdata", "AndDev is Cool!"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
} catch (ClientProtocolException e) {
Log.e(TAG,e.toString());
} catch (IOException e) {
Log.e(TAG,e.toString());
}
}
The problem is that the httpclient.execute(..) line takes around 1.5 to 3 seconds, and I do not understand why. Just requesting a page with HTTP Get takes around 80 ms or so, so the problem doesn't seem to be the network latency itself.
The problem doesn't seem to be on the server side either, I have also tried POSTing data to http://www.disney.com/ with similarly slow results. And Firebug shows 1 ms response time when POSTing data to my server locally.
This happens on the Emulator and with my Nexus One (both with Android 2.2).
If you want to look at the complete code, I've put it on GitHub.
It's just a dummy program to do HTTP Post in the background using AsyncTask on the push of a button. It's my first Android app, and my first java code for a long time. And incidentially, also my first question on Stackoverflow ;-)
Any ideas why httpclient.execute(httppost) takes so long?
Allright, I solved this myself with some more investigation. All I had to do was to add a parameter that sets the HTTP Version to 1.1, as follows:
HttpParams params = new BasicHttpParams();
params.setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
HttpClient httpclient = new DefaultHttpClient(params);
I found this thanks to the very nice HttpHelper Class from and-bookworm and some trial-and-error.
If I remember correctly, HTTP 1.0 opens a new TCP connection for every request. Does that explain the large delay?
A HTTP POST request now takes between 50 and 150 ms over WLAN and something between 300 and 500 ms over 3G.
i am not on android, but i faced exactly the same kind of problem on windows platform with httpclient 4.0.1, after quite a bit of head scratching, i found the solution.
HttpParams params = new BasicHttpParams();
//this how tiny it might seems, is actually absoluty needed. otherwise http client lags for 2sec.
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
HttpClient httpClient = new DefaultHttpClient(params);
HttpResponse httpResponse;
HttpPost httpPost = new HttpPost("http://"+server+":"+port+"/");
StringEntity entity = new StringEntity(content, "utf-8");
entity.setContentType("text/plain; charset=utf-8");
httpPost.setEntity(entity);
httpResponse=httpClient.execute(httpPost);
String response = IOUtils.toString(httpResponse.getEntity().getContent(),encoding);
httpResponse.getEntity().consumeContent();
httpClient.getConnectionManager().shutdown();
return(response);
i have no idea why setting the parameters with HTTP1.1 version solves the problem. but it does.
also even weirder, the symptom did not show if executing an HTTP Get request.
anyhow, i hope this helps some out there !
h