This question already has an answer here:
How to set content-length in android?
(1 answer)
Closed 9 years ago.
I've set almost all my data :
String capcha = editText.getText().toString();
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("myurl.asp");
// Add your data
String str = "param1=x¶m2=y";
StringEntity strEntity = new StringEntity(str);
httppost.setEntity(strEntity);
httppost.setHeader("Set-Cookie", sessionCookie);
httppost.setHeader("Accept", "text/html");
httppost.setHeader("Content-Type", "application/x-www-form-urlencoded");
// httppost.setHeader("Content-Length", ); HOW TO GET CONTENT LENGTH ?
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
Thanks for your help.
Perhaps this may help to solve your problem:
httppost.setHeader("Content-Length", String.valueOf(strEntity.getContentLength()));
Related
This question already has answers here:
How to set header in httppost/ httpget request
(2 answers)
Closed 5 years ago.
I am trying to implement a simple JSON get and post to the URL that sends token as header. Can someone help me with passing the token in header as http get and http post?
if (api.method.equals("GET")) {
try {
HttpGet httpget = new HttpGet(SERVER_URL + api.toString()+URL.replaceAll(" ","%20").trim());
//httpget.getParams().setParameter(CoreProtocolPNames.USE_EXPECT_CONTINUE, Boolean.FALSE);
httpget.setHeader("Content-type", "application/json");
httpget.setHeader("Authentication", "mytokenvalue");
//List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
//nameValuePairs.add(new BasicNameValuePair("Content-type", "application/json"));
//httpget.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
Log.d("111111111",SERVER_URL+api.toString()+URL.replaceAll(" ","%20").trim());
HttpResponse response = httpclient.execute(httpget);
return readStream(response.getEntity().getContent());
} catch (HttpHostConnectException e) {
e.printStackTrace();
}
}
HttpClient client = new DefaultHttpClient();
HttpPost request = new HttpPost(SERVER_URL
+ api.toString()+URL.replaceAll(" ","%20").trim());
request.setHeader(new BasicHeader("Content-Type","application/json"));
request.setHeader(newBasicHeader("Authorization,"token=mytokenvalue"));
response = client.execute(request);
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
The app worked fine before adding a certificate from startSSL.com to my server.
The POST array is empty on the server. And I don't get any error, I get response from the server.
This is the code I use to make my server calls in AsyncTasks.
// Making HTTP request
try {
// check for request method
if(method == "POST"){
// request method is POST
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new UrlEncodedFormEntity(params));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
}else if(method == "GET"){
// request method is GET
DefaultHttpClient httpClient = new DefaultHttpClient();
//String paramString = URLEncodedUtils.format(params, "utf-8");
// url += "?" + paramString;
for (NameValuePair nameValuePair : params) { // modificado para usar URIS
url += "/" + nameValuePair.getValue();
}
Log.d("Intentando conectar con el servidor: ", url);
HttpGet httpGet = new HttpGet(url);
HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
}
Any idea?
It was a server isue. Removing the 'www' part from the url solved it. I configured my server with no www but I didn't imagine it was going to make me sweat this way.
This question already has answers here:
Send Post request along with HttpHeaders on Android
(2 answers)
Closed 2 years ago.
I am passing post data as Json using postUrl() to WebView in Android. Now I want to pass a header "Content-Type: application/json" also along with that. How to achieve it?
may this will help u :)
HttpPost httpPost = new HttpPost(url_src);
HttpParams httpParameters = new BasicHttpParams();
httpclient.setParams(httpParameters);
StringEntity se = new StringEntity(str,"UTF-8");
se.setContentType("application/json");
httpPost.setEntity(se);
httpPost.setHeader("userId", ""+userId);//userID
httpPost.setHeader("machineId", machineId);//deviceUserMachineID
response = httpclient.execute(httpPost);
I believe this will help: link
Doesn't look like you can actually send headers with postUrl(), only with loadUrl() method.
it possible to post data with header i have done it in my project
I am posting you my code ...
HttpParams par = new BasicHttpParams();
//par.setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
int timeoutConnection = 30000;
HttpConnectionParams.setConnectionTimeout(par, timeoutConnection);
int timeoutSocket = 10000;
HttpConnectionParams.setSoTimeout(par, timeoutSocket);
DefaultHttpClient httpClient = new DefaultHttpClient(par);
// httpClient.setParams(par);
HttpPost httpPost = new HttpPost(url);
httpPost = new HttpPost(url);
if(postMessage==null)
throw new IllegalArgumentException("please send post data as well");
byte[] data =postMessage.toString().getBytes("UTF-8");
String base64 = Base64.encodeToString(data, Base64.DEFAULT);
System.out.println("Base64: "+base64);
// httpPost.setEntity(new UrlEncodedFormEntity(params));
httpPost.setEntity(new StringEntity(base64));
httpPost.setHeader("token", app.getToken());
httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
System.out.println("httpEntity.getContent():"+ is );
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"));
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
data = sb.toString();
//remember httpconnection should be in background thread
// if you use asynchtask then upper code should be in doinbackground method
//and in onpostexecution do data load in webview
webview.loadData(data, "text/html","UTF-8");
This question already has answers here:
Android HttpPost: how to get the result
(5 answers)
Closed 9 years ago.
I'm using
HttpPost httppost = new HttpPost(url);
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs,"UTF-8"));
in order to send data to my webservice. How could I get the http response code (like 200, 404, 500, etc) ?
I'm not able to use getResponseCode() from HttpURLConnection
More of your code would be helpful here but the way I do it is this:
HttpClient client = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs,"UTF-8"));
HttpResponse response = client.execute(httppost);
int responseCode = response.getStatusLine().getStatusCode()
by
HttpResponse response = httpclient.execute(httppost);
StatusLine statusLine = response.getStatusLine();
int code = statusline.getStatusCode();
This is the code done in C# for posting an XML Document Element
XmlString = #"<WOITEMS><WOITEM ACTION='I'>" + TransData + "</WOITEM></WOITEMS>";
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(XmlString);
saveRegisterItems(xmlDoc.DocumentElement);
saveRegisterItems is a WCF service method which receives a Document Element as it's parameter. How can I do that in Android using HttpPOST? I tried the below code. But, it doesn't work.
HttpResponse response = null;
String myUrl = "http://"+Constants.strURL+"/ServiceOrders.svc/SaveRegisterItems";
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(myUrl);
StringEntity se = new StringEntity(XmlString, HTTP.UTF_8);
se.setContentType("text/xml");
httpPost.setHeader("Content-Type", "application/soap+xml;charset=UTF-8");
httpPost.setEntity(se);
I get response.getStatusLine() as "HTTP/1.1 200 OK" but, it's not updated in the server. I think, passing a XML Document Element will do it. Please Help
I got the solution when I changed the second parameter of httpPost.setHeader() method. It should like this.
httpPost.setHeader("Content-Type", "application/xml;charset=UTF-8");
I got proper response by giving EntityUtils.toString(entity).