How to post array parameters with HttpComponents - java

I want to perform this command with Apache http-components (4.1.2)
curl --data "strings[]=testOne&string-keys[]=test.one&strings[]=testTwo&string-keys[]=test.two&project=Test" https://api.foo.com/1/string/input-bulk
The target api need strings and string-keys parameters as array, which mean repeating strings[] and string-keys[] for each parameter.
This curl command works fine but with Http-component, while I got exactly the same parameters.
Maybe I'm doing something wrong.
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add( new BasicNameValuePair( "project", PROJECT_NAME ) );
for ( Entry entry : newEntries )
{
params.add( new BasicNameValuePair( "string-keys[]", entry.getKey() ) );
params.add( new BasicNameValuePair( "strings[]", entry.getValue() ) );
params.add( new BasicNameValuePair( "context[]", "" ) );
}
URI uri = URIUtils.createURI( "https", "api.foo.com", -1, "/1/string/input-bulk", null, null );
UrlEncodedFormEntity paramEntity = new UrlEncodedFormEntity( params );
logger.info( "POST params : {}", EntityUtils.toString( paramEntity ) );
HttpPost httpRequest = new HttpPost( uri );
httpRequest.setEntity( paramEntity );
HttpResponse response = new DefaultHttpClient().execute( httpRequest );
The POST params looks like :
POST params : project=Test&string-keys%5B%5D=test.one&strings%5B%5D=TestOne&string-keys%5B%5D=test.two&strings%5B%5D=TestTwo
If I put them behind --data in curl, it works, but not with HttpCoponents.
Can someone explain me why?
Thanks in advance

Try adding the header "application/x-www-form-urlencoded" in your httpRequest
httpRequest.addHeader("content-type", "application/x-www-form-urlencoded");
HttpResponse response = new DefaultHttpClient().execute( httpRequest );
Hopefully that will work

Related

Equivalent of PostMethod of Java in PHP

I would like to ask if there exists a PHP function to simulate this block of code in Codeigniter.
HttpClient httpClient = new HttpClient();
PostMethod postMethod = new PostMethod(requestURL);
NameValuePair[] datas = {new NameValuePair("studentnumber", studentnumber),
new NameValuePair("studentdata", encryptedData)};
postMethod.setRequestBody(datas);
int statusCode = httpClient.executeMethod(postMethod);
byte[] responseByte = postMethod.getResponseBody();
String responseBody = new String(responseByte, "UTF-8");
curl doesn't seem to work, while $this->output->set_output passes the data properly but fails to catch the response of the requestUrl.
Thank you.
I was able to catch the response from requestUrl using this block of code that I found on HTTP POST from PHP, without cURL (thanks a lot for this).
$options = array(
'http' => array(
'method' => "POST",
'header' => "Accept-language: en\r\n" . "Content-type: application/x-www-form-urlencoded\r\n",
'content' => http_build_query(array(
'studentnumber' => $studentnumber,
'studentdata' => $encryptedData,
),'','&'
)
));
$refno = file_get_contents($requestUrl,false,$context);

Java get webpage source or timeout

I'm trying to fetch data from a webpage, but if the page isn't available the program runs for a long time until it timeouts. I need it to try to get the webpage for 10 seconds and if it doesn't get a response in that time it returns null. How can I make it to work that way, please?
Here is how I get the data:
public int getThreadData( String address ) throws IOException{
String valueString = null;
URL url = new URL( "http://" + address + ":8080/web-console/ServerInfo.jsp" );
URLConnection urlConnection = url.openConnection();
urlConnection.setRequestProperty( "User-Agent",
"Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.3)
Gecko/20100401" );
BufferedReader br = new BufferedReader( new InputStreamReader
( urlConnection.getInputStream(), "UTF-8" ) );
String inputLine;
while ( ( inputLine = br.readLine() ) != null )
{
if ( inputLine.contains( "#Threads" ) )
{
valueString = inputLine.substring( inputLine.indexOf( "/b>" ) + 3 );
valueString = valueString.substring( 0, valueString.indexOf( "<" ) );
}
}
br.close();
return Integer.parseInt( valueString );
}
Have you tried setting the connection timeout like following:
urlConnection.setConnectTimeout(10000); // 10000 milliseconds
You should probably use a HTTP library (like Apache's HTTPClient) that can simplify these things vastly. If you were using HTTPClient, you would do something like this:
// Set the timeout to 20-seconds.
final HttpParams httpParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParams, 20 * 1000);
HttpConnectionParams.setSoTimeout(httpParams, 20 * 1000);
DefaultHttpClient httpClient = new DefaultHttpClient(cm, httpParams);
HttpPost postRequest = new HttpPost(URL);
HttpResponse response;
response = httpClient.execute(postRequest);

Translate a Web Service POST from Java to .NET

Below is a snipped from a Java Client that connects to a website and uploads a file via the POST method. I have to reproduce this client in a Visual Studio environment, but I don't see any equivalent functions in the .NET environment for the setEntity() function used in the Java.
Everything I've found points to using this...
public void uploadFile(File uploadFile, String partner, String key,
String baseUrl,boolean isPartner) throws IOException {
DefaultHttpClient httpclient = new DefaultHttpClient();
httpclient.getParams().setParameter(
CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1
);
String url = baseUrl + "?" + (isPartner ? "partnerId" : "ori") + "="
+ partner.toUpperCase() + "&authKey="
+ key+ "&key="
+ key;
HttpPost httppost = new HttpPost(url);
MultipartEntity multipartEntity = new MultipartEntity();
ContentBody contentBody = new FileBody(uploadFile, "text/xml");
multipartEntity.addPart("dataFile", contentBody);
httppost.setEntity(multipartEntity);
HttpResponse response;
response = httpclient.execute(httppost);
HttpEntity resEntity = response.getEntity();
if (resEntity != null) {
resEntity.consumeContent();
}
httpclient.getConnectionManager().shutdown();
}
Everything I've found in Visual studio uses something like this below for the POST method. The WebRequest object has no obvious way of adding the parameters I need.
Dim request As WebRequest = WebRequest.Create("http://Test.com/import?partnerId=2&authKey=XdUa")
request.Method = "POST"
Dim postData As String = StrData
Dim byteArray As Byte() = Encoding.UTF8.GetBytes(postData)
request.ContentType = "dataStr"
request.ContentLength = byteArray.Length
Dim dataStream As Stream = request.GetRequestStream()
dataStream.Write(byteArray, 0, byteArray.Length)
dataStream.Close()
Dim response As WebResponse = request.GetResponse()
Any guidance would be greatly appreciated. If my question is not clear, let me know, I'll try again.
You can add following code snippet to add the parameter
request.ContentType="application/x-www-form-urlencoded"
Dim postData As String = "name1="+value1+"&name2="+value2
Dim byteArray As Byte() = Encoding.UTF8.GetBytes(postData)
Rest will remain same.

Simulate clicking a submit button in Java

I am writing the program in Java, and i want to fill out the specified fields of form, simulate submit clicking, so get the result page. I am testing my idea on the url http://stu21.kntu.ac.ir/Login.aspx that have two fields txtusername and txtpassword. I am trying the code as follow but it does not return the result page for me. How can i do it ? What do i wrong in this code ?
DefaultHttpClient conn = new DefaultHttpClient();
conn = new DefaultHttpClient();
ArrayList<NameValuePair> pairs = new ArrayList<NameValuePair>();
pairs.add(new BasicNameValuePair("txtusername", "8810103"));
pairs.add(new BasicNameValuePair("txtpassword", "8810103"));
HttpPost httpPost = new HttpPost("http://stu21.kntu.ac.ir/Login.aspx");
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(pairs,
"UTF-8");
httpPost.setHeader(
"UserAgent",
"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.19 (KHTML, like Gecko) Ubuntu/12.04 Chromium/18.0.1025.151 Chrome/18.0.1025.151 Safari/535.19");
httpPost.setEntity(entity);
HttpResponse response = conn.execute(httpPost);
InputStream is = response.getEntity().getContent();
RandomAccessFile raf = new RandomAccessFile(
"/home/hossein/Desktop/random.aspx", "rw");
raf.seek(0);
int bytes = 0;
byte[] buffer = new byte[1024];
while ((bytes = is.read(buffer)) != -1)
raf.write(buffer, 0, bytes);
raf.close();
is.close();
Sorry if my question duplicates with another threads, i can not find my solution on other threads.
Thanks In Advance :)
I think you need HTTPUnit. There is a good tutorial at javaworld.
Just look at the following example there :
public void testGoodLogin() throws Exception {
WebConversation conversation = new WebConversation();
WebRequest request = new GetMethodWebRequest( "http://www.meterware.com/servlet/TopSecret" );
WebResponse response = conversation.getResponse( request );
WebForm loginForm = response.getForms()[0];
request = loginForm.getRequest();
request.setParameter( "name", "master" );
// "clicking the button" programatically
response = conversation.getResponse( request );
assertTrue( "Login not accepted",
response.getText().indexOf( "You made it!" ) != -1 );
assertEquals( "Page title", "Top Secret", response.getTitle() );
}
I am sure that you can do your testing just like this.

Encode params used within a post

I need to encode the params to ISOLatin which i intend to post to the site. I'm using org.apache.http. libraries. My code looks like follows:
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost("www.foobar.bar");
post.setHeader("Content-Type", "application/x-www-form-urlencoded");
HttpParams params = new BasicHttpParams();
params.setParameter("action", "find");
params.setParameter("what", "somebody");
post.setParams(params);
HttpResponse response2 = httpClient.execute(post);
Thank you!
You are setting parameters wrong. Here is an example,
PostMethod method = new PostMethod(url);
method.addParameters("action", "find");
method.addParameters("what", "somebody");
int status = httpClient.executeMethod(method);
byte[] bytes = method.getResponseBody();
response = new String(bytes, "iso-8859-1");
if (status != HttpStatus.SC_OK)
throw new IOException("Status code: " + status + " Message: "
+ response);
The default encoding will be Latin-1.

Categories

Resources