我需要用Java复制Postman POST。 通常我只需要在URL中创建一个只有params的HttpPost,因此很容易构建:
HttpClient client = HttpClientBuilder.create().build();
HttpPost post = new HttpPost("someUrls.com/upload");
ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
postParameters.add(new BasicNameValuePair("username", username));
postParameters.add(new BasicNameValuePair("password", password));
postParameters.add(new BasicNameValuePair("owner", owner));
postParameters.add(new BasicNameValuePair("destination", destination));
try{
post.setEntity(new UrlEncodedFormEntity(postParameters, Consts.UTF_8));
HttpResponse httpResponse = client.execute(post);
//Do something
}catch (Exception e){
//Do something
}
但是,如果我有一个像下面的图像那样在URL和身体中有Params的POST,我该怎么办? 现在我正在制作像这样的HttpPost:
protected function createRequest(
$method,
$content,
$uri = '/test',
$server = ['CONTENT_TYPE' => 'application/json'],
$parameters = [],
$cookies = [],
$files = []
) {
$request = new \Illuminate\Http\Request;
return $request->createFromBase(
\Symfony\Component\HttpFoundation\Request::create(
$uri,
$method,
$parameters,
$cookies,
$files,
$server,
$content
)
);
}
但我如何把&#34; filename&#34;和&#34; filedata&#34;身体中的参数与URL中的参数一起? 实际上我使用org.Apache库,但我也可以考虑其他库。
感谢任何有帮助的人!
答案 0 :(得分:0)
你可以使用下面的代码在POST方法调用中将body参数作为“application / x-www-form-urlencoded”传递
package han.code.development;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.URL;
import javax.net.ssl.HttpsURLConnection;
public class HttpPost
{
public String getDatafromPost()
{
BufferedReader br=null;
String outputData;
try
{
String urlString="https://www.google.com"; //you can replace that with your URL
URL url=new URL(urlString);
HttpsURLConnection connection=(HttpsURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.addRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.addRequestProperty("Authorization", "Replace with your token"); // if you have any accessToken to authorization, just replace
connection.setDoOutput(true);
String data="filename=file1&filedata=asdf1234qwer6789";
PrintWriter out;
if((data!=null))
{
out = new PrintWriter(connection.getOutputStream());
out.println(data);
out.close();
}
System.out.println(connection.getResponseCode()+" "+connection.getResponseMessage());
br=new BufferedReader(new InputStreamReader(connection.getInputStream()));
StringBuilder sb=new StringBuilder();
String str=br.readLine();
while(str!=null)
{
sb.append(str);
str=br.readLine();
}
outputData=sb.toString();
return outputData;
}
catch(Exception e)
{
e.printStackTrace();
}
return null;
}
public static void main(String[] args)
{
HttpPost post=new HttpPost();
System.out.println(post.getDatafromPost());
}
}
答案 1 :(得分:0)
答案 2 :(得分:0)
我已经解决了这个问题:
这里的代码......
async def on_message(self, message):
print(message)
while True:
try:
#print('ws_connections: ', self.ws_connection, self.ws_connection.stream.socket)
_fut = self.write_message(self.users[self].request_data())
except tornado.iostream.StreamClosedError as e:
print('StreamClosedError:', e)
break
except tornado.websocket.WebSocketClosedError as e:
print('WebSocketClosedError:', e)
break
except KeyError as e:
print('KeyError:', e)
break
await gen.sleep(1)