XMLhttprequest from android to get response from ATG server - json

I am facing one problem with android accessing ATG sessionconfirmationNumber using rest API, I am using http urlconnection to achive the same, Code is working perfectly and I am getting output also, but the result in the Json response is rounded as the number is large. below are the code.
try {
StrictMode.ThreadPolicy policy = new StrictMode.
ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
// Defined URL where to send data
// URL url = new URL("http://10.201.62.27:8080/Test-Servlet/TestServletAndroid?"+data);
URL url = new URL("http://52.70.41.98:7203/rest/model/atg/rest/SessionConfirmationActor/getSessionConfirmationNumber");
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
HttpURLConnection httpConnection = (HttpURLConnection) conn;
Log.e("Connection", conn.toString());
httpConnection.setRequestMethod("GET");
httpConnection.setRequestProperty("Content-Type", "text/plain");
httpConnection.connect();
// Get the server response
reader = new BufferedReader(new InputStreamReader(httpConnection.getInputStream()));
StringBuilder sb = new StringBuilder();
String line = null;
// Read Server Response
while ((line = reader.readLine()) != null) {
// Append server response in string
sb.append(line + "\n");
}
response = sb.toString();
Log.v("ResponseVALID", response);
JSONObject json = new JSONObject(response);
Log.v("Tag0", json.toString());
scn = json.getLong("sessionConfirmationNumber");
/*String srr[] = response.split(":");
for(int i =0;i<srr.length;i++){
Log.v("TAG2:",srr[i]);
}
SCN=srr[1].replace("}", "");
SCN= SCN.trim();
Log.v("SCN ",SCN);
scn=Long.parseLong(SCN);*/
} catch (Exception ex) {
ex.printStackTrace();
} finally {
try {
reader.close();
} catch (Exception ex) {
}
}
I have faced the same problem with angularjs on the web-portal, XMLHttp request helped me out to achieve the goal.
Can I use XMLHttp request from android studio?, if yes!, Please give me some reference as I have tried and nothing got worked out for me.
Any help would be appreciated.

I believe the line:
httpConnection.setRequestProperty("Content-Type", "text/plain");
need to be:
httpConnection.setRequestProperty("Content-Type", "application/json");

Related

My Windows Phone app Get empty response (404 Not Found) Scond time, work's great first time;And always work fine if without SSL

I am building my first windowsPhone 8.1 application ,the role of my application is to create connection with server to get information from it, so I am writing the code to do this process by sending json-rpc request to server to get some information ,I am successful to get it in first time but when I send the second request I am receiving an empty response with 404 error (page not found).
But when I call the service without https (http only) it works fine regardless how many time I call it !
public async Task<string> GetDataFromServer(string urlToCall, string JSONData,string RR)
{
string UserName = “XXXXXXX”
string Password = "XXX";
using ( var handler = new HttpClientHandler())
{
handler.Credentials = new NetworkCredential(UserName, Password);
HttpClient client = new HttpClient(handler);
HttpResponseMessage response = null;
try
{
response = await client.PostAsync(urlToCall, new StringContent(JSONData.ToString(), Encoding.UTF8, " application/json"));
string res = response.Content.ReadAsStringAsync().Result;
Windows.UI.Popups.MessageDialog g = new Windows.UI.Popups.MessageDialog(res);
await g.ShowAsync();
return res;
}
catch (Exception ex)
{
Windows.UI.Popups.MessageDialog g = new Windows.UI.Popups.MessageDialog("Error is : " + ex.Message);
g.ShowAsync();
return "Error";
}
finally
{
response.Dispose();
client.CancelPendingRequests();
client.Dispose();
handler.Dispose();
}
}
}
Again, when call the URL of service (start with https) on first time I got response with seeked data, but second time I receive an empty response with 404 error (page not found) !!
Any help please
Please try to use this solution.
public async Task<string> SendJSONData3(string urlToCall, string JSONData)
{
string UserName = "XXXXXXXXX";
string Password = "XXXXXXXXX";
var httpWebRequest = (HttpWebRequest)WebRequest.Create(urlToCall);
httpWebRequest.Credentials = new NetworkCredential(UserName, Password);
httpWebRequest.ContentType = "text/json";
httpWebRequest.Method = "POST";
using (var streamWriter = new StreamWriter(await httpWebRequest.GetRequestStreamAsync()))
{
string json = JSONData;
streamWriter.Write(json);
streamWriter.Flush();
}
var httpResponse = (HttpWebResponse)await httpWebRequest.GetResponseAsync();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
var result = streamReader.ReadToEnd();
return result;
}
}
A couple of ideas:
Do not use the .Result property. Just use await instead to avoid deadlocks.
Remove the additional space in front of the media type parameter " application/json"
Enable logging on the webserver and see if the second request arrives on the server.
Get a network trace, for example with Wireshark or Fiddler.
Try puting WebRequest.RegisterPrefix("https://", WebRequestCreator.ClientHttp); in your initialization code, as proposed in this answer.

http web server return zip file in a windows phone 8

hello i'm write a http web server in my app.
I used this code
http://developer.nokia.com/community/wiki/A_simplistic_HTTP_Server_on_Windows_Phone
this procedure works but not have a extentions of file in http response
return a file name without extention (.zip)
private async Task<StringBuilder> HandleRequest(StreamSocket socket)
{
//Initialize IO classes
DataReader reader = new DataReader(socket.InputStream);
reader.InputStreamOptions = InputStreamOptions.Partial;
DataWriter writer = new DataWriter(socket.OutputStream);
writer.UnicodeEncoding = Windows.Storage.Streams.UnicodeEncoding.Utf8;
//handle actual HTTP request
String request = await StreamReadLine(reader);
string[] tokens = request.Split(' ');
if (tokens.Length != 3)
{
throw new Exception("invalid http request line");
}
string httpMethod = tokens[0].ToUpper();
string httpUrl = tokens[1];
//read HTTP headers - contents ignored in this sample
while (!String.IsNullOrEmpty(await StreamReadLine(reader))) ;
try
{
if (httpUrl == "DOWNLOADZIP")
{
using (IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication())
{
String content = null;
Stream stream;
byte[] data;
IsolatedStorageFileStream sri = storage.OpenFile("CompressedFiles.zip", FileMode.Open, FileAccess.Read);
if (null != sri)
{
stream = sri;
data = new byte[stream.Length];
stream.Read(data, 0, data.Length);
writer.writebytes(data);
}
}
}
}
catch (Exception ex)//any exception leads to an Internal server error
{
writer.WriteString("HTTP/1.0 500 Internal server error\r\n");
writer.WriteString("Connection: close\r\n");
writer.WriteString("\r\n");
writer.WriteString(ex.Message);
}
}
await writer.StoreAsync();//write data actually to the network interface
socket.Dispose();
return null;
}
this is a solutions
ret.AppendLine("HTTP/1.0 200 OK");
ret.AppendLine("Content-Type: text/html");
ret.AppendLine("Connection: close");
ret.AppendLine("");
ret.AppendLine("Content-Disposition: attachment; filename=myfile.zip");

How to send POST request with parameters asynchronously in windows phone 8

I would like to send POST request in windows phone 8 environment my code is running successfully but i am getting NotFound exception. Its mean is i want to POST some data but i am sending null. So please let me know how to send POST Request asynchronously with Data in windows phone 8 environmet. I tried following links but not helpful.
link link2
I approached like this
private async Task<LastRead> SyncLastReadPOST(LastRead lastreads, bool actionStatus)
{
string jsondata = "";
actionStatus = false;
apiData = new LastReadAPI()//It is global variable from apiData this object has the information
{
AccessToken = thisApp.currentUser.AccessToken,
Book = lastreads.Book,
Page = lastreads.Page,
Device = lastreads.Device
};
jsondata = Newtonsoft.Json.JsonConvert.SerializeObject(apiData);
LastRead responsedData = new LastRead();
Uri lastread_url = new Uri(string.Format("{0}lastread", url_rootPath));
try
{
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(lastread_url);
webRequest.ContentType = "application/json";
webRequest.Accept = "application/json;odata=verbose";
webRequest.Method = "POST";
webRequest.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback), webRequest);
}
catch { }
return responsedData;
}
private void GetRequestStreamCallback(IAsyncResult ar)
{
HttpWebRequest request = (HttpWebRequest)ar.AsyncState;
Stream postStream = request.EndGetRequestStream(ar);
var input = Newtonsoft.Json.JsonConvert.SerializeObject(jsondata);//jsondata is my global data variable in json format.
byte[] byteArray = Encoding.UTF8.GetBytes(input);
postStream.WriteAsync(byteArray, 0, byteArray.Length);
postStream.Close();
request.BeginGetResponse(new AsyncCallback(GetResponseStreamCallback), request);
}
private void GetResponseStreamCallback(IAsyncResult ar)
{
try
{
HttpWebRequest webRequest = (HttpWebRequest)ar.AsyncState;
HttpWebResponse response;
//In following line i am getting the exception notFound.
response = (HttpWebResponse)webRequest.EndGetResponse(ar);
Stream streamResponse = response.GetResponseStream();
StreamReader streamReaders = new StreamReader(streamResponse);
var responces = streamReaders.ReadToEnd();
streamResponse.Close();
streamReaders.Close();
response.Close();
}
catch(Exception ex)
{
}
}
As far as i know notFound exceptions comes when we are not posting any data while using the POST request method. you can see i have mentioned the data i am passing into the GEtRequestStreamCallback. I have mentioned a note. Please help me. Where i am going to wrong.
Try setting the content-type to application/json; charset=utf-8
Also, you can do all that stuff in nicer and shorter way(sample):
var wc = new WebClient();
//SET AUTH HEADER IF NECESSARY
//wc.Headers["Authorization"] = "OAUTH "+TOKEN;
wc.Headers["Content-Type"] = "application/json;charset=utf-8";
wc.UploadStringCompleted += (s, er) =>
{
if (er.Error != null) MessageBox.Show("Error\n" + er.Error);
else MessageBox.Show(er.Result);
};
string data = JsonConvert.SerializeObject(MY_DATA_OBJECT);
MessageBox.Show(data);
wc.UploadStringAsync(new Uri(POST_URI), "POST", data);
I did it with the help of HttpClient inplace of WebClient. Following few lines will do magic. :)
HttpClient hc = new HttpClient();
hc.BaseAddress = new Uri(annotation_url.ToString());
HttpRequestMessage req = new HttpRequestMessage(HttpMethod.Post, myUrl);
HttpContent myContent = req.Content = new StringContent(myJsonString, Encoding.UTF8, "application/json");
var response = await hc.PostAsync(myUrl, myContent);
//Line for pull out the value of content key value which has the actual resposne.
string resutlContetnt = response.Content.ReadAsStringAsync().Result;
DataContractJsonSerializer deserializer_Json = new DataContractJsonSerializer(typeof(MyWrapperClass));
MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(resutlContetnt.ToString()));
AnnotateResponse = deserializer_Json.ReadObject(ms) as MyWrapperClass;

WebBrowser control NavigateToString() is unable to fire ajax call for embedded web page

My requirement is to show a dynamic web page in windows phone app.
The code snippet :
private string MYACCOUNT_URL = "http://www.abc.com"
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
try
{
//Initialization
HttpWebRequest WebReq = (HttpWebRequest)WebRequest.Create(MYACCOUNT_URL);
WebReq.CookieContainer = cookies;
//Our method is post, otherwise the buffer (postvars) would be useless
WebReq.Method = "POST";
//We use form contentType, for the postvars.
WebReq.ContentType = "application/x-www-form-urlencoded";
//WebHeaderCollection headers = new WebHeaderCollection();
//headers[HttpRequestHeader.UserAgent] = useragent;
//WebReq.Headers = headers;
//WebReq.Headers["User-Agent"] = useragent;
//var userAgent = WebReq.GetType().GetProperty("UserAgent");
//if (null != userAgent)
//{
// userAgent.SetValue(WebReq, useragent, null);
//}
//WebReq.UserAgent = userAgent.ToString();
Cookie emailCookie = new Cookie("email", Constants.LoginUserName);
WebReq.CookieContainer.Add(new Uri(MYACCOUNT_URL), emailCookie);
Cookie userIdCookie = new Cookie("id", Constants.UserId);
WebReq.CookieContainer.Add(new Uri(MYACCOUNT_URL), userIdCookie);
Cookie tokenCookie = new Cookie("token", Constants.AccessToken);
WebReq.CookieContainer.Add(new Uri(MYACCOUNT_URL), tokenCookie);
WebReq.BeginGetResponse(new AsyncCallback(MyAccountResponseCallback), WebReq);
}
catch (WebException ex)
{ }
}
private void MyAccountResponseCallback(IAsyncResult asynchronousResult)
{
try
{
HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asynchronousResult);
Stream streamResponse = response.GetResponseStream();
StreamReader streamRead = new StreamReader(streamResponse);
string responseString = streamRead.ReadToEnd();
responseString = Regex.Replace(responseString, "<!DOCTYPE html>", "", RegexOptions.IgnoreCase).Trim();
responseString = Regex.Replace(responseString, "<html lang=\"en\">", "<html>", RegexOptions.IgnoreCase).Trim();
responseString = Regex.Replace(responseString, "<meta charset=\"utf-8\"", "<meta", RegexOptions.IgnoreCase).Trim();
if (response.StatusCode == HttpStatusCode.OK)
{
Dispatcher.BeginInvoke(() =>
{
web_browser.NavigateToString(responseString);
});
}
streamResponse.Close();
streamRead.Close();
// Release the HttpWebResponse
response.Close();
}
catch (WebException ex)
{
}
}
The web page is loaded properly. Web page contains forms, jquery-ajax. These events(click/load) are getting triggered from the loaded web page. The same web page is getting loaded in Internet Explorer and working fine with all the events on the windows phone 8.
Please suggest any solution. Thanks in advance.
Thanks for your response. unfortunately "IsScriptEnabled" is not helpful here. But I got a solution. I am using "Navigate" method instead of "NavigateToString" and it is working. It is loading the web page and also it let me do all events present on web page.
Thanks

HttpWebRequest Post method error in Windows phone 8

HttpWebRequest in Windows phone 8
I am developing a windows phone 8 app using c#/xaml. I am facing some issues with httpwebrequest class. I want to download data from server using post method.But httpwebrequest is not working as expected. It is returning error (The remote server returned an error: NotFound) when i try to call webservices subsequently. What could be the reason?. Please help...Following is my code.
string response = "";
httpwebrequest = WebRequest.Create(new Uri(serviceurl)) as HttpWebRequest;
httpwebrequest.Method = "POST";
httpwebrequest.ContentType = "application/json";
byte[] data = Serialization.SerializeData(request);
using (var requestStream = await Task<Stream>.Factory.FromAsync(httpwebrequest.BeginGetRequestStream, httpwebrequest.EndGetRequestStream, null))
{
await requestStream.WriteAsync(data, 0, data.Length);
}
response = await httpRequest(httpwebrequest);
var result = Serialization.Deserialize<T>(response);
return result;
}
public async Task<string> httpRequest(HttpWebRequest request)
{
try
{
string received;
using (var response = (HttpWebResponse)(await Task<WebResponse>.Factory.FromAsync(request.BeginGetResponse, request.EndGetResponse, null)))
{
using (var responseStream = response.GetResponseStream())
{
using (var sr = new StreamReader(responseStream))
{
received = await sr.ReadToEndAsync();
}
}
response.Close();
}
return received;
}
catch(Exception ex)
{
return "";
}
}
Have you checked the inner exception? Because i had a similar problem an when i checked the inner exception i found that i got another error from the server. Check also the real http status code into the response header.