gpt4 book ai didi

java - 如何在 POST 请求时在 HTTP header 中设置自定义变量

转载 作者:行者123 更新时间:2023-12-01 15:15:02 24 4
gpt4 key购买 nike

I want to add my Machine Name to the http request header as a custom variable in JAVA .

Here is my code :

private void sendIssuesToWebService(Issue issue) {
// TODO Auto-generated method stub

System.out.println("\n\n");
//LOG.info("Sending issue : "+issue.getKey());


HttpURLConnection ycSend = null;
BufferedReader in1 = null;
JSONObject j = null ;
try{

URL urlSend = null;

try{
urlSend = new URL(targetURL+"Issue/ImportIssue");
}
catch (MalformedURLException e) {
// TODO Auto-generated catch block

LOG.severe(" URL Malformed Exception while authentication ");
e.printStackTrace();
}



try{
ycSend = (HttpURLConnection) urlSend.openConnection();
}
catch (IOException e) {
// TODO Auto-generated catch block

LOG.severe(" Error in Http URL Connection while authentication");
e.printStackTrace();
}


ycSend.setDoOutput(true);

ycSend.setRequestMethod("POST");
ycSend.setRequestProperty("Content-Type", "application/json");


try{

OutputStreamWriter outSend = null;


outSend = new OutputStreamWriter(ycSend.getOutputStream());
outSend.write(IssueAndHistoryJsonStr);


outSend.flush();

}catch(Exception e)
{
LOG.severe("Cannot write output stream while sending issue ");

}

System.out.println( ycSend.getResponseCode()+"\n\n\n");

if(ycSend.getResponseCode()!=200){

in1 = new BufferedReader(new InputStreamReader(
ycSend.getErrorStream()));


String inputLine;
while ((inputLine = in1.readLine()) != null)
inputResponse=inputResponse+inputLine;
//System.out.println("$$$$$$$"+inputResponse);

try{
j = new JSONObject(inputResponse);
LOG.info(j.get("errors").toString());
}
catch(Exception e){



String errorTitle = inputResponse.substring(inputResponse.indexOf("<title>")+7,inputResponse.indexOf("</title>"));

LOG.severe(errorTitle);



}

LOG.severe(" Error in Sending an Issue to the web service and the issue key is "+issue.getKey());
//LOG.severe("Error is : "+j.get("errors"));
}
else
{

LOG.info("Issue "+issue.getKey()+" Sent successfully" );
countIssues++;
System.out.println("\n\n");
}
//LOG.info("Issue ***** " +issue.getKey()+ " sent with response : "+inputResponse);




}

catch(Exception e)
{
LOG.severe(" Error in Sending an Issue to the web service and the issue key is "+issue.getKey());
//LOG.severe(yc.getResponseMessage());
//LOG.severe("Error is : "+j.get("errors"));
e.printStackTrace();
}

}

最佳答案

我相信您只是想添加另一个类似于内容类型的 requestProperty。

ycSend.setRequestProperty("Machine-Name", MachineName);

关于java - 如何在 POST 请求时在 HTTP header 中设置自定义变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11741347/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com