Following is the code snippet in JAVA.
I want to do the similar thing in C#, Metro apps.
public static String callWebservice(String paramJson) throws Exception {
...
...
DefaultHttpClient httpclient = new DefaultHttpClient();
...
if (requestType.equals("POST") ) {
HttpPost httpPostReq = new HttpPost(url);
StringEntity se = new StringEntity(jsonParam.toString());
se.setContentType("application/json;charset=UTF-8");
se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json;charset=UTF-8"));
httpPostReq.setEntity(se);
httpresponse = httpclient.execute(httpPostReq);
}
else if (requestType.equals("GET") ) {
HttpGet httpGetReq = new HttpGet(url);
...
}
else if (requestType.equals("PUT") ) {
HttpPut httpPutReq = new HttpPut(url);
....
}
else if (requestType.equals("PATCH") ) {
HttpPatch httpPatchReq = new HttpPatch(url);
...
}
responseText = EntityUtils.toString(httpresponse.getEntity());
return responseText;
}
I wonder if there are separate methods of Java: ("HttpPost", "HttpGet", "HttpPut", "HttpPatch" ) in C# also.
I tried to accomplish the task using HttpClient but I'm facing errors while adding headers "Accept", "Content-Type", "X-Security-AuthKey" and throws error like:
Misued header ...
But somehow I managed to add header using "TryAddWithoutValidation".
In my C# code, I have the code snippet like following:
private async Task<string> CallHttpServiceHelper(string json)
{
try
{
...
HttpModel model = JsonConvert.DeserializeObject<HttpModel>(json);
try
{
JsonObject jsonObject = new JsonObject();
jsonObject["userName"] = JsonValue.CreateStringValue(model.paramss.userName);
jsonObject["password"] = JsonValue.CreateStringValue(model.paramss.password);
jsonObject["domain"] = JsonValue.CreateStringValue(model.paramss.domain);
jsonObject["accessKey"] = JsonValue.CreateStringValue(model.paramss.accessKey);
inputParams = jsonObject.Stringify();
}
catch (Exception ex)
{
}
url = model.url;
requestType = model.requesttype;
try
{
authenticationKey = model.authenticationKey;
}
catch (Exception ex)
{
}
if (requestType == "POST")
{
uri = new Uri(url);
data = new StringContent(inputParams, Encoding.UTF8, "application/json");
if (authenticationKey != null && authenticationKey != "")
{
data.Headers.Add("X-Security-AuthKey", authenticationKey);
}
httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Add("Accept", "application/json");
httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/json");
httpClient.Timeout = TimeSpan.FromMinutes(10);
var response = await httpClient.PostAsync(uri, data);
content = await response.Content.ReadAsStringAsync();
httpClient.Dispose();
}
else if (requestType == "GET")
{
uri = new Uri(url);
httpClient = new HttpClient();
if (authenticationKey != null && authenticationKey != "")
{
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("X-Security-AuthKey", authenticationKey);
}
httpClient.DefaultRequestHeaders.Add("Accept", "application/json");
httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/json");
httpClient.Timeout = TimeSpan.FromMinutes(10);
var response = await httpClient.GetAsync(uri);
content = await response.Content.ReadAsStringAsync();
httpClient.Dispose();
}
else if (requestType == "PUT")
{
...
}
else if (requestType == "PATCH")
{
...
}
return content;
}
catch (Exception ex)
{
return "fail";
}
}
This code in C#, Windows RT Apps works fine for POST method. But I'm not getting the required response from webservice in GET method. In GET method I have to call WebService by passing three Headers only "Accept", "Content-Type", "X-Security-AuthKey".
I don't know what I'm doing wrong.
Related
Why 3.1 can request data normally, but using 4.5 will throw Circular redirect
Do I need any additional configuration to make it compatible with 3.1?
In 4.5 I set setCircularRedirectsAllowed and setMaxRedirects but it still throws an exception
httpclient3.1:
private static Document requestData(String url, CookieStore cookieStore, String proxyIp, String proxyPort, String userAgent) {
HttpClient hc = new HttpClient();
List<Header> headers = new ArrayList<Header>();
headers.add(new Header("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3"));
headers.add(new Header("Accept-Language", "zh-CN,zh;q=0.9"));
headers.add(new Header("Cache-Control", "max-age=0"));
headers.add(new Header("connection", "keep-alive"));
StringBuilder tmpcookies = new StringBuilder();
for (Cookie cookie : cookieStore.getCookies()) {
tmpcookies.append(cookie.getName()).append("=").append(cookie.getValue()).append(";");
}
headers.add(new Header("Cookie", tmpcookies.toString()));
headers.add(new Header("Host", "Host"));
headers.add(new Header("Upgrade-Insecure-Requests", "1"));
headers.add(new Header("User-Agent", userAgent));
hc.getHostConfiguration().getParams().setParameter("http.default-headers", headers);
hc.getHostConfiguration().setProxy(proxyIp, Integer.parseInt(proxyPort));
hc.getHttpConnectionManager().getParams().setConnectionTimeout(10000);
hc.getHttpConnectionManager().getParams().setSoTimeout(10000);
GetMethod get = new GetMethod(url);
try {
int code = hc.executeMethod(get);
if (code == 200) {
InputStream inputStream = get.getResponseBodyAsStream();
String result = StringUtil.inputStreamToString(inputStream);
return Jsoup.parse(result);
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
httpclient4.x :
private static Document requestData(String url, CookieStore cookieStore, RequestConfig defaultRequestConfig, String userAgent, String uniSocCreCode) {
CloseableHttpClient hc = HttpClients.custom().build();
HttpGet get = new HttpGet(url);
get.addHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3");
get.addHeader("Accept-Language", "zh-CN,zh;q=0.9");
get.addHeader("Cache-Control", "max-age=0");
get.addHeader("connection", "keep-alive");
StringBuilder tmpcookies = new StringBuilder();
for (Cookie cookie : cookieStore.getCookies()) {
tmpcookies.append(cookie.getName()).append("=").append(cookie.getValue()).append(";");
}
get.addHeader("Cookie", tmpcookies.toString());
get.addHeader("Host", "Host");
get.addHeader("Upgrade-Insecure-Requests", "1");
get.addHeader("User-Agent", userAgent);
get.setConfig(defaultRequestConfig);
try {
CloseableHttpResponse response = hc.execute(get);
if (response.getStatusLine().getStatusCode() == 200) {
InputStream inputStream = response.getEntity().getContent();
String result = StringUtil.inputStreamToString(inputStream);
return Jsoup.parse(result);
}
} catch (Exception e) {
log.error("error", e);
}
return null;
}
error(4.x):
Caused by: org.apache.http.client.CircularRedirectException: Circular redirect to 'https://www.qcc.com/firm/a4eb893fb989c8cdd7a73809d5b65715.html'
at org.apache.http.impl.client.DefaultRedirectStrategy.getLocationURI(DefaultRedirectStrategy.java:193)
at org.apache.http.impl.client.DefaultRedirectStrategy.getRedirect(DefaultRedirectStrategy.java:223)
at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:126)
at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:185)
... 68 common frames omitted
Still think it is a version problem
move to org.apache.httpcomponents.client5 » httpclient5 solved the problem
I hava a java apache client written as :
public JSONObject createProject(String accessToken) throws PostEditException {
JSONObject jobj = new JSONObject();
HttpClient client = HttpClientBuilder.create().build();
HttpPost post = new HttpPost("https://"+HOST_TRANSPORT+"/api/projects/create");
try{
post.addHeader("Content-Type","application/json");
post.addHeader("Accept","application/json");
String authorizationValue = "Bearer "+accessToken;
post.addHeader("Authorization",authorizationValue);
HttpResponse response = client.execute(post);
if (response == null) {
System.out.println("this is null");
}
int code = response.getStatusLine().getStatusCode();
if (code != 200) {
HttpEntity entity = response.getEntity();
ContentType contentType = ContentType.getOrDefault(entity);
Charset charset = contentType.getCharset();
if (charset == null) {
charset = Charset.forName("UTF-8");
}
String errorBody = EntityUtils.toString(entity, charset);
log.info("createProject: Unable to process request. HTTP code: " + code + " responseBody: " + errorBody);
throw new PostEditException("createProject: Unable to process createProject request. HTTP code: " + code + " responseBody: " + errorBody);
} else {
HttpEntity entity = response.getEntity();
ContentType contentType = ContentType.getOrDefault(entity);
Charset charset = contentType.getCharset();
if (charset == null) {
charset = Charset.forName("UTF-8");
}
String taggedResult = EntityUtils.toString(entity, charset);
jobj = new JSONObject(taggedResult) ;
}
}catch (NoRouteToHostException re) {
log.error("createProject: unable to route to host."+re);
throw new PostEditException("createProject: unable to route to host.",re);
}catch (IOException ie) {
log.error("createProject: problem executing HTTP request."+ ie);
throw new PostEditException("createProject: problem executing HTTP request.",ie);
}catch (Exception e) {
log.error("createProject: an error occurred." +e );
throw new PostEditException("createProject: an error occurred",e);
} finally {
post.releaseConnection();
}
return jobj;
} //createProject
so , I am trying to write a test case to mock the HttpClient.
so I Wrote the Mockito test case as :
PowerMockito.mockStatic(HttpClientBuilder.class);
HttpClientBuilder builderMock = Mockito.mock(HttpClientBuilder.class);
PowerMockito.doReturn(builderMock).when(HttpClientBuilder.class, "create");
HttpClient clientMock = Mockito.mock(CloseableHttpClient.class);
CloseableHttpResponse responseMock = Mockito.mock(CloseableHttpResponse.class);
Mockito.doReturn(clientMock).when(builderMock).build();
Mockito.doReturn(responseMock).when(clientMock).execute(Mockito.any());
classname obj= new classname();
Method m = classname.class.getDeclaredMethod("createProject", String.class);
JSONObject result =(JSONObject) m.invoke(obj,"accesstoken");
But when I debug the program, I see the real httpclient is created instead of the mocked one. How Do I create the mock httpclient so that I can mock the status and response as well.
I want to write put Curl to java:
curl -X PUT -u username:password http://localhost:80/api/client/include/clientID
Thats what I googled but my problem is that how can I pass the value of client_id and client to put since there is an /include between them. I am a bit confused of how to write a curl. Can any one help me?
public String RestPutClient(String url, int newValue, int newValue2) {
// example url : http://localhost:80/api/
DefaultHttpClient httpClient = new DefaultHttpClient();
StringBuilder result = new StringBuilder();
try {
HttpPut putRequest = new HttpPut(url);
putRequest.addHeader("Content-Type", "application/json");
putRequest.addHeader("Accept", "application/json");
JSONObject keyArg = new JSONObject();
keyArg.put("value1", newValue);
keyArg.put("value2", newValue2);
StringEntity input;
try {
input = new StringEntity(keyArg.toString());
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
return success;
}
putRequest.setEntity(input);
HttpResponse response = httpClient.execute(putRequest);
if (response.getStatusLine().getStatusCode() != 200) {
throw new RuntimeException("Failed : HTTP error code : "
+ response.getStatusLine().getStatusCode());
}
BufferedReader br = new BufferedReader(new InputStreamReader(
(response.getEntity().getContent())));
String output;
while ((output = br.readLine()) != null) {
result.append(output);
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return result.toString();
}
I assume the parameters you are passing are supposed to represent client and clientID. You simply need to build the URL your passing to HttpPut from your parameters.
If these are your parameters
url = "http://localhost:80/api/";
newValue = "client";
newValue2 = "clientID";
then your HttpPut initialization would look like this
HttpPut putRequest = new HttpPut(url + newValue + "/include/" + newValue2);
Also see:
How do I concatenate two strings in Java?
I use this httpclient to post a image to my grails as follows. How do I receive the file in grails?
public static String webPost(String method, File data, Context c) throws Exception {
String json = "";
if (isOnline(c) == true) {
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response;
HttpPost httppost ;
try {
httppost = new HttpPost(method);
MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
entity.addPart("image", new FileBody(data));
httppost.setEntity(entity);
response = httpclient.execute(httppost);
if (response != null) {
HttpEntity r_entity = response.getEntity();
json = EntityUtils.toString(r_entity);
Log.i("ja", json);
}
} catch (Exception e) {
throw new RuntimeException(e.getLocalizedMessage());
} finally {
httpclient = null;
response = null;
httpclient = null;
}
} else {
throw new RuntimeException("No internet connection");
}
return json;
}
My grails:
def image = request.getFile('image')
image.transferTo(new File('c:/p.png') )
Error:
groovy.lang.MissingMethodException: No signature of method: org.apache.catalina.core.ApplicationHttpRequest.getFile() is applicable for argument types: (java.lang.String) values: [image]
Possible solutions: getXML(), getAt(java.lang.String), getAt(java.lang.String), getLocale(), getInfo(), recycle()
at mclient.TestController$_closure1.doCall(TestController.groovy:10)
at mclient.TestController$_closure1.doCall(TestController.groovy)
at java.lang.Thread.run(Thread.java:662)
I'm trying to write a POST call to Google Translate with Jersey 1.5. This is my code:
package main;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.core.util.MultivaluedMapImpl;
import javax.ws.rs.core.MultivaluedMap;
public class Main {
private static String GOOGLE_TRANSLATE_URL = "https://www.googleapis.com/language/translate/v2";
private static String translateString(String sourceString, String sourceLanguage, String targetLanguage) {
String response;
Client c = Client.create();
WebResource wr = c.resource(GOOGLE_TRANSLATE_URL);
MultivaluedMap<String, String> params = new MultivaluedMapImpl();
params.add("q", sourceString);
params.add("source", sourceLanguage);
params.add("target", targetLanguage);
params.add("key", "xxxx");
wr.header("X-HTTP-Method-Override", "GET");
response = wr.post(String.class, params);
return response;
}
public static void main(String[] args) {
System.out.println(translateString("Hello", "en", "sv"));
}
}
When I run this, all I get back is this: com.sun.jersey.api.client.UniformInterfaceException: POST https://www.googleapis.com/language/translate/v2 returned a response status of 404.
I've managed to accomplish this with a simple cURL command like so:
curl --header "X-HTTP-Method-Override: GET" -d key=xxxx -d q=Hello -d source=en -d target=sv https://www.googleapis.com/language/translate/v2
Thanks in advance!
I suspect that POST with zero Content-Length is not something a normal HTTP server will accept. The RFC does not define this case, but the main assumption of POST is that you're sending a message body.
Looking at the Google API, they mention the following
You can also use POST to invoke the API if you want to send more data in a single request. The q parameter in the POST body must be less than 5K characters. To use POST, you must use the X-HTTP-Method-Override header to tell the Translate API to treat the request as a GET (use X-HTTP-Method-Override: GET).
This means that instead of adding q, source and target parameters in the URL, you need to do so in the POST body. I'm not familiar with the Jersey API, from a brief look you just need to add params as an explicit second parameter to the .post call, remove the queryParams() call, and set the Content-Length properly.
I think the best and correct way is this
private static final String gurl = "www.googleapis.com";
private static final String gpath = "/language/translate/v2/detect";
public String detectLangGooglePost(String text) throws SystemException {
List<NameValuePair> qparams = new ArrayList<NameValuePair>();
qparams.add(new BasicNameValuePair("key", key));
URI uri;
try {
uri = URIUtils.createURI("https", gurl, -1, gpath, URLEncodedUtils.format(qparams, "UTF-8"), null);
} catch (URISyntaxException e) {
throw new SystemException("Possibly invalid URI parameters", e);
}
HttpResponse response = getPostResponse(uri, text);
StringBuilder builder = getBuilder(response);
String language = getLanguage(builder);
return language;
}
private HttpResponse getPostResponse(URI uri, String text) throws SystemException {
List<NameValuePair> qparams = new ArrayList<NameValuePair>();
qparams.add(new BasicNameValuePair("q", text));
HttpResponse response;
HttpClient httpclient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(uri);
httpPost.addHeader("X-HTTP-Method-Override", "GET");
try {
httpPost.setEntity(new UrlEncodedFormEntity(qparams));
response = httpclient.execute(httpPost);
} catch (Exception e) {
throw new SystemException("Problem when executing Google get request", e);
}
int sc = response.getStatusLine().getStatusCode();
if (sc != HttpStatus.SC_OK)
throw new SystemException("google status code : " + sc);
return response;
}
private StringBuilder getBuilder(HttpResponse response) throws SystemException {
HttpEntity entity = response.getEntity();
if (entity == null)
throw new SystemException("response entity null");
StringBuilder builder = new StringBuilder();
BufferedReader in = null;
String str;
try {
in = new BufferedReader(new InputStreamReader(entity.getContent()));
while ((str = in.readLine()) != null)
builder.append(str);
} catch (IOException e) {
throw new SystemException("Reading input stream of http google response entity problem", e);
} finally {
IOUtils.closeQuietly(in);
}
if (builder.length() == 0)
throw new SystemException("content stream of response entity empty has zero length");
return builder;
}
private String getLanguage(StringBuilder builder) throws SystemException {
JSONObject data = null;
JSONArray detections = null;
String language = null;
JSONObject object = (JSONObject) JSONValue.parse(builder.toString());
if (object == null)
throw new SystemException("JSON parsing builder object returned null");
if (object.containsKey("data") == false)
throw new SystemException("JSONObject doesn't contain data key");
data = (JSONObject) object.get("data");
detections = (JSONArray) data.get("detections");
if (detections == null)
throw new SystemException("JSON detections is null");
JSONObject body = (JSONObject) ((JSONArray) detections.get(0)).get(0);
if (body == null)
throw new SystemException("detections body is null");
if (body.containsKey("language") == false)
throw new SystemException("language key is null");
language = (String) body.get("language");
if (language == null || language.equals(unknown))
throw new SystemException("Google lang detection - resulting language : " + language);
return language;
}
I was able to send very long text like this!
Client:
MultivaluedMap<String,String> formData = new MultivaluedMapImpl();
formData.add("text", text);
WebResource resource = Client.create().resource(getBaseURI()).path("text2rdf");
return resource.type("application/x-www-form-urlencoded").post(String.class, formData);
Server:
#POST
#Produces("text/whatever")
public String textToRdf (
#FormParam("text") String text) {...
I switched to Apache HttpClient 4.x and solved it like this instead:
public class Main {
private static String GOOGLE_TRANSLATE_URL = "https://www.googleapis.com/language/translate/v2";
private static String GOOGLE_API_KEY = "xxxx";
private static String translateString(String sourceString, String sourceLanguage, String targetLanguage) {
String response = null;
// prepare call
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(GOOGLE_TRANSLATE_URL+"?q="+sourceString+"&source="+sourceLanguage+"&target="+targetLanguage+"&key="+GOOGLE_API_KEY);
post.setHeader("X-HTTP-Method-Override", "GET");
try {
// make the call
ResponseHandler<String> responseHandler = new BasicResponseHandler();
response = client.execute(post, responseHandler);
} catch (IOException e) {
// todo: proper error handling
}
return response;
}
public static void main(String[] args) {
System.out.println(translateString("hello", "en", "sv"));
}
}
Don't really know why this works better than Jersey, but it works. Thanks for trying to help!